1.简单示例:
1.1新建一个待测的类:
1.2创建以上类的测试类:
以上为一个简单的测试用例。
编译好测试用例之后,就可以使用NUnit GUI (.NET 2.0)的测试界面来。
首先打开测试界面,
其次引用进刚才编译好的测试用例(dll,exe等)
出来一下效果:
2.NUnit的核心功能:
2.1 Equality Asserts,用来判定测试结果是否与预期一致
公式: Assert.AreEqual( object expected, object actual );
例子:NUnit.Framework.Assert.AreEqual(10, vTestResult),其中vTestResult为一个变量,它可以通过运行指定的测试情况来获取相应的值。
特殊情况:当指定的值需考虑精度的时候,可以设置容错的范围。
Assert.AreEqual( double expected, double actual, double tolerance );
Assert.AreEqual( 10.9F, 10.1F, 0.8F );可以正常结束
Assert.AreEqual( 10.9F, 10.1F, 0.7F );不能正常结束
出错信息为:
2.2 Identity Asserts 判断两个对象是否相同
公式: Assert.AreSame( object expected, object actual );//判定两个对象是否相同
Assert.AreNotSame( object expected, object actual );//判定两个对象是否不相同
Assert.Contains( object anObject, IList collection );//判定一个对象是否包含在一组数据里
例子:
DataTable vDT1 = new DataTable("FirstDT");
DataTable vDT2 = vDT1.Copy();
vDT2.TableName = "SecondDT";
DataTable vDT3 = vDT1;
ArrayList vList = new ArrayList();
vList.Add(vDT2);
NUnit.Framework.Assert.AreSame(vDT1, vDT3);
NUnit.Framework.Assert.AreNotSame(vDT1, vDT2);
NUnit.Framework.Assert.Contains(vDT2, vList);
特殊情况:当判断两个对象是否相同,最好不同对象以不同的名称命名。
如果之上的vDT2.TableName没有进行命名,并以以下方法判定,那出错信息将没有任何意义。
NUnit.Framework.Assert.Contains(vDT1, vList);
出错信息为:
2.3 Condition Tests 条件断言
公式: Assert.IsTrue( bool condition );//判断执行结果是否为真
Assert.IsFalse( bool condition);//判断执行结果是否为假
Assert.IsNull( object anObject );//判断执行结果是否为空
Assert.IsNotNull( object anObject );//判断执行结果是否为非空
Assert.IsNaN( double aDouble );//(Not a Number)的意思,一般只有当aDouble=double.NaN的时候才能实现。
Assert.IsEmpty( string aString );//判断指定值是否为空字符串 ※但是绝对不是Null
Assert.IsNotEmpty( string aString );//判定指定值是否为非空,即有值。
Assert.IsEmpty( ICollection collection );//判定所有列表组成是否为空
例:
string vstrbychar = "";
NUnit.Framework.Assert.IsEmpty(vstrbychar.ToCharArray());
string[] vstres = new string[2];
NUnit.Framework.Assert.IsEmpty(vstres);
ArrayList vList = new ArrayList();
NUnit.Framework.Assert.IsEmpty(vList );
Assert.IsNotEmpty( ICollection collection );//判定所有列表组成是否为非空
2.4 Comparisons 比较断言
公式:Assert.Greater( int arg1, int arg2 );//判断arg1比arg2大,arg1为执行结果,arg2为预期的范围;※注意必须是大于,不包含等于。
自己可以试试 Assert.Greater(1, 2);执行测试程序,将会出现 Excepted:greater then 2;But was:1的错误
Assert.GreaterOrEqual( int arg1, int arg2 );//判断arg1大于等于arg2
Assert.Less( int arg1, int arg2 );//判断arg1小于arg2
Assert.LessOrEqual( int arg1, int arg2 );//判断arg1小于等于arg2
2.5 Type Asserts 类型断言
公式:Assert.IsInstanceOfType( Type expected, object actual );//判断actual指定实例是否为expected类型
例:在当前测试类内,实现如下断言:
NUnit.Framework.Assert.IsInstanceOfType(typeof(AccountTest3), this);//AccountTest3为当前类的类名,this表示当前测试类的实例
Assert.IsNotInstanceOfType( Type expected, object actual );//判断actual指定实例不是expected类型
Assert.IsAssignableFrom( Type expected, object actual );//判断actual是否由expected类型生成的实例;
例:Assert.IsAssignableFrom(typeof(object), "word");//这个断言将会失败,因为字符串"word"是string类型,而不是object类型创建的
Assert.IsNotAssignableFrom( Type expected, object actual );//判断actual是否不由expected类型生成的实例;
2.6 Utility Methods 实用方法(主要用来自定义断言)
公式:Assert.Fail();//当不使用断言时,可以直接使用Fail方法来提交测试失败
Assert.Fail( string message );//提交失败时,显示失败的原因等信息
例:if(vPath.Equal("")){Assert.Fail("Path is not right!")}
Assert.Ignore();//忽略,主要为了暂时不进行测试或者指定条件可以不予考虑,但是测试结果会在测试界面以黄色显示
2.7 StringAssert 字符串断言
公式:StringAssert.Contains( string expected, string actual );//指定字符串包含预期的值 ※注意这里是区分大小写字母的,也区分全角半角字符
StringAssert.StartsWith( string expected, string actual );//指定字符串起始于预期的值
StringAssert.EndsWith( string expected, string actual );//指定字符串以预期的值结束
StringAssert.AreEqualIgnoringCase( string expected, string actual );//判断两个字符串是否相同。※不区分大小写,但是区分全角和半角
StringAssert.IsMatch( string expected, string actual );//判断实际执行的结果是否与预期的格式匹配
例:NUnit.Framework.StringAssert.IsMatch(@"^\d{3}[A-Z]{2}", "991XX");
//具体正则表达式请参考 http://www.cnblogs.com/si812cn/archive/2008/10/10/1307792.html
2.8 CollectionAssert 列表集断言
公式:CollectionAssert.AllItemsAreInstancesOfType( IEnumerable collection,Type expectedType );//判定所有的列表项都属于expectedType类型
例:
ArrayList vList2 = new ArrayList();
vList2.Add("string");
vList2.Add("123");
CollectionAssert.AllItemsAreInstancesOfType( vList2, typeof(string) );
如果以上的vList2.Add("123")改成vList2.Add(123);则将测试失败。
CollectionAssert.AllItemsAreNotNull( IEnumerable collection );//所有列表项均为非空
例:
ArrayList vList3 = new ArrayList();
vList3.Add(null);
vList3.Add("123");
CollectionAssert.AllItemsAreNotNull( vList3);
以上测试将失败,因为第一项为空。
CollectionAssert.AllItemsAreUnique( IEnumerable collection );//所有列表项是唯一的 ※区分大小写,区分全角和半角
例:
ArrayList vList4= new ArrayList();
vList4.Add(1);
vList4.Add(2);
vList4.Add(1);
CollectionAssert.AllItemsAreUnique( vList4);
以上测试将失败,因为第一项与第三项重复出现了。
CollectionAssert.AreEqual( IEnumerable expected, IEnumerable actual );
//按照顺序比较两个列表内各项值是否一致 ※包括类型,大小写,全半角,NULL也可以比较
CollectionAssert.AreEquivalent( IEnumerable expected, IEnumerable actual);
//与CollectionAssert.AreEqual不同,它可以是没有顺序的,但是一项只能对应一项,不能一个值与多个相同值比较
CollectionAssert.AreNotEqual( IEnumerable expected, IEnumerable actual );//两列不相同
CollectionAssert.AreNotEquivalent( IEnumerable expected, IEnumerable actual );//两列不等价
CollectionAssert.Contains( IEnumerable expected, object actual );//执行结果包含在预期的列表中
CollectionAssert.DoesNotContain( IEnumerable expected, object actual );//执行结果不被包含在预期的列表中
CollectionAssert.IsSubsetOf( IEnumerable subset, IEnumerable superset );
//subset内的列表项都出现在superset中,则表示subset是superset的子集 ※包含两个列表集数据数相同
例:
ArrayList vList7 = new ArrayList();
vList7.Add(1);
vList7.Add("123");
ArrayList vList8 = new ArrayList();
vList8.Add(1);
vList8.Add("123");
vList8.Add(null);
CollectionAssert.IsSubsetOf( vList7, vList8 );//该断言将可以通过,反过来则不能通过
如果vList8.Add(null);去除掉
CollectionAssert.IsSubsetOf( vList7, vList8 );//该断言将可以通过,反过来也可以通过,因为根据子集的概念,他俩相互都可以是对方的子集
CollectionAssert.IsNotSubsetOf( IEnumerable subset, IEnumerable superset);//判断不是子集
CollectionAssert.IsEmpty( IEnumerable collection );//判断列表值是否为空值,即已定义,未赋值
例:
ArrayList vList9 = new ArrayList();
CollectionAssert.IsEmpty(vList9);//执行成功
ArrayList vList10 = new ArrayList();
vList10.Add("");
CollectionAssert.IsEmpty(vList10);//执行失败
结果如下:
2.9 FileAssert 文件断言
公式:FileAssert.AreEqual( Stream expected, Stream actual );//判断两个文件流一致
2.10 Constraint-Based Assert Model 约束断言模式
公式:Assert.That( object actual, IConstraint constraint ) //执行结果与指定的约束值对比
例:
Assert.That( myString, Is.EqualTo("Hello") );//Is属性存在于NUnit.Framework.SyntaxHelpers命名空间内
Assert.That( myString, new EqualConstraint("Hello") );
Assert.That( 2.3, Is.GreaterThan( 2.0 ) & Is.LessThan( 3.0 ) );//可以组合使用
备注:NUnit.Framework.SyntaxHelpers里包含了IS,Has,Text,List,ListMapper属性进行一系列的操作。
.Net中的map类有Dictionary、SortedDictionary、HashTable、SortedList等,SortedDictionary提供了排序支持。
Has里主要有Has.Perporty(vName)判断是否有相应的属性
Has.Count(int),Has.Length(int)
List Mapper:可以对列表内每个项指定预期值
string[] strings = new string[] { "a", "ab", "abc" };
int[] lengths = new int[] { 1, 2, 3 };
Assert.That(List.Map(strings).Property("Length"), Is.EqualTo(lengths));
Assert.That(new ListMapper(strings).Property("Length"),Is.EqualTo(lengths));
2.11 Attributes 属性设置
2.11.1 CategoryAttribute 分类属性:目的是把各个测试用例分成几块,进行分别测试
例:
以上两个测试用例可以在NUnit- GUI里可选择地测试:
你可以在以上的画面里选择step1进行测试,也可以选择step2进行测试,当然也可以全选进行测试。具体方法就是点击【add】添加到【Selected Categories】里。
2.11.2 CultureAttribute 文化属性:目的是用来指定特定的文化前提下才进行相应的测试,如果当前文化不是指定的时候,则不进行测试。
例:
以上代码指定ShowDateTimeWithFormat测试用例只在中国文化下才进行测试。
※这个与接下来要讲的【SetCulture】属性有所区别,SetCulture指的是主观设定当前文化为指定的文化进行测试。
比如我们在日文系统下面,要测试该测试用例是否在中文的环境下也能通过,就可以使用【SetCulture】
2.11.3 Description 描述属性:目的是在输出保存为测试结果时附加测试类信息
例:[TestFixture, Description("Fixture description here")]
public class Test1
2.11.4 ExpectedExceptionAttribute 报错时捕捉属性:在测试用例上指定该属性,如果测试用例报出了该属性指定的错误,则测试通过,否则测试失败。
例:
以上InsufficientFundsException为待测试工程中创建的一个出错类,这里用它来指定如果该测试用例执行了指定的动作,并报出该错误,说明测试是正确的。
以上屏蔽掉的属性增强了对出错信息的捕捉,可以指定除了该类型的错误,还限制必须是指定的出错信息或者包含指定的信息。
2.11.5 ExplicitAttribute 明确指定属性:该属性用来规定只有明确指定执行该测试用例时才执行。
比如该测试用例包含【Category("Step1")】属性,运行时也按照2.11.1进行了设置,则可以执行。如果在NUnit-gui里选择全部运行,则不会被执行。
例:[Test, Explicit]
public void TestCase1()
{
//-----
}
2.11.6 IgnoreAttribute 忽略测试属性
当测试条件不存在或者特殊情况不能实现测试时,可以设置该属性。设置该属性的结果是测试状态变为黄色以警示。
例:
结果为:
2.11.7 PlatformAttribute 操作平台属性:限定当前测试用例针对的操作平台或者语言版本
例:
第二个测试用例表示在win98,WinME下不对该测试用例进行测试。
结果:
2.11.8 PropertyAttribute :该属性主要在输出结果时作为附加信息用
2.11.9 SetCultureAttribute 设置文化属性:为了测试各种文化语言下测试用例能否通过。具体参见2.11.2
2.11.10 SetUpAttribute :该属性在设置了该属性以及【TearDown】以外的测试用例每次运行之前均会执行。
2.11.11 TearDownAttribute :该属性在设置了该属性以及【Setup】属性以外的测试用例每次运行之后均会执行。
2.11.12 TestFixtureSetUpAttribute,TestFixtureTearDownAttribute:以上2.11.10,2.11.11之前添加【TestFixture】之后,
就不是每个测试用例运行前或运行后都执行了,只在最开始时执行一次,结束之前执行一次。
2.11.13 TestAttribute :该属性标识对应方法为测试用例。
2.11.14 TestFixtureAttribute :该属性标识对应类为测试类。
3 特定操作:
通过NUnit-GUI可以用除了【Category】以外的方法测试选择的测试用例
具体操作为:在测试界面上,右键弹出里勾选【Show CheckBoxes】就可以了。