仿Nunit的【TEST】属性测试一段程序
2008-10-12 20:57
375 查看
做一个WEB服务项目时,经常要调试一段函数是否执行正确。经自己研究Nunit,自己编写了一个小型的测试工具:
一:添加一个custom attribute named "TestAttribute" the code is:
[AttributeUsage(AttributeTargets.All,AllowMultiple=false)]
public class TestAttribute :System.Attribute
{
public TestAttribute()
{
}
}
二:创建一个Console程序:
三:然后在你想测试的方法前面加一个属性[Test()]
这样就可以测试了!
例如:
[Test()]
public void xx(string aa,string bb)
{
Console.WriteLine("xx method");
Console.WriteLine(aa);
Console.WriteLine(bb);
}
第一次写文章,写得很差。呵呵!
一:添加一个custom attribute named "TestAttribute" the code is:
[AttributeUsage(AttributeTargets.All,AllowMultiple=false)]
public class TestAttribute :System.Attribute
{
public TestAttribute()
{
}
}
二:创建一个Console程序:
class Program { static void Main() { RunTestMethod(null); } static void RunTestMethod(Assembly assembly) { assembly = Assembly.GetExecutingAssembly(); Type[] types=assembly.GetTypes(); for (int i = 0; i < types.Length; i++) { Type type = types[i]; foreach (MethodInfo method in type.GetMethods()) { object[] atrList = method.GetCustomAttributes(typeof(TestAttribute), false); if (atrList.Length >= 1) { object instance = Activator.CreateInstance(type); object[] parameters = new object[2]; parameters[0] = "panbo"; parameters[1] = "ningjiao"; try { method.Invoke(instance, parameters); } catch(Exception e) { Console.WriteLine(e.Message); } } } } Console.ReadKey(); }
三:然后在你想测试的方法前面加一个属性[Test()]
这样就可以测试了!
例如:
[Test()]
public void xx(string aa,string bb)
{
Console.WriteLine("xx method");
Console.WriteLine(aa);
Console.WriteLine(bb);
}
第一次写文章,写得很差。呵呵!
相关文章推荐
- 用NUnit为.NET程序做测试 --- 常用测试属性之一 SetUp TearDown TestFixtureSetUp TestFixtureTearDown
- 用NUnit为.NET程序做测试 --- 常用测试属性之二 Ignore, Explicit
- 用NUnit为.NET程序做测试 --- 常用测试属性之三 Category
- 001test 测试程序
- 一段程序代码 --- 实现解析字典,自动转换生成属性。
- net自动化测试之道API测试-例子程序:ApiTest
- 测试某一段程序运行时间的方法
- TestPort 1.0 用perl写的来测试端口的小程序
- 用NUnit为.NET程序做测试---数据库测试之二 数据操作测试
- Netbean下测试Applet程序时,出现“警告:不能读取 AppletViewer 的属性文件”
- 001test 测试程序
- c# 计算程序执行时间,计算一段代码执行所用的时间,测试效率
- python读北大corpor的一段测试程序
- 使用NUnit进行DotNet程序测试
- NUnit的使用(2)——实际测试过程中,程序代码和测试代码是分开的
- 用Nunit测试通讯程序
- VS2015测试某一段程序执行时间
- 测试一段程序代码执行时间的方法
- stat获取文件属性+原创测试程序
- 基于ONVIF协议的(IPC)客户端程序开发-1:测试工具(ONVIF Device Test Tool)