转贴于: http://blog.csdn.net/cpcpc/article/details/6185862
二、基于约束的断言模式(Constraint-Based Assert Model)
基于约束的断言模式只有一种方式来完成所有的断言,即Assert.That([],[]). 第二个参数才是完成断言的重要逻辑。
在VS2005中使用此模式需引用三个命名空间: using NUnit.Framework;
using NUnit.Framework.Constraints;
using NUnit.Framework.SyntaxHelpers;
1、Equal Constraint
用于测试当前值与期望值相等。如Assert.That(myvalue,Is EqualTo(Expectedvalue)); 如果两个不同数值类型的值相同则测试也会通过。float 与double的比较需要另外添加一个参数来指出两者之间的允许公差,在此公差之类则会测试通过。如:Assert.That( 2.1 + 1.2, Is.EqualTo( 3.3 ).Within( .0005 );. 当然在NUnit2.4.2之后,Within 表示的公差也可用于别的类型之间的比较。
另外,float的NaN值也可以跟NaN值比较,Assert.That( double.NaN, Is.EqualTo( double.NaN ) );此时测试通过。
要比较两个相同维度、相同元素、相同类型的数组,需要加上AsCollection修饰符,即Assert.That( array2x2, Is.EqualTo( array4 ).AsCollection ); 如果不加则测试失败。
如果比较两个字符类型,且需要忽略大小则添加IgnoreCase修饰符,同时也可以用于字符型数组或集合。
如:Assert.That( actual, Is.EqualTo( expected ).IgnoreCase;
2、Same As Constraint
用于测试两个对象是否相同,前面已经说过相同的概念,同Assert.AreSame()/Assert.AreNotSame()。如:
[Test]
public void SameAsTest()
{
//定义变量
var ex1 = new Exception();
var ex2 = ex1;
var ex3 = new Exception();
//约束语法
Assert.That(ex2, Is.SameAs(ex1));
Assert.That(ex3, Is.Not.SameAs(ex1));
//使用继承语法
Expect(ex2, SameAs(ex1));
Expect(ex3, Not.SameAs(ex1));
}
3、Condition Constraint
条件测试提供了测试一个对象是否为空、引用为空、是否为真等行为。如:
Assert.That( anObject, Is.Null );
Assert.That( anObject, Is.Not.Null );
Assert.That( condition, Is.True );
Assert.That( condition, Is.False );
Assert.That( aDouble, Is.NaN );
Assert.That( aDouble, Is.Not.NaN );
Assert.That( aString, Is.Empty );
Assert.That( collection, Is.Empty );
Assert.That( collection, Is.Unique );
Expect( aDouble, NaN );
Expect( collection, Unique );
4、Comparsion Constraint
两个数值之间的比较。如:
Assert.That(7, Is.GreaterThan(3)); //大于
Assert.That(7, Is.GreaterThanOrEqualTo(3)); //大于等于
Assert.That(7, Is.AtLeast(3)); //最少
Assert.That(7, Is.GreaterThanOrEqualTo(7));
Assert.That(7, Is.AtLeast(7));
Assert.That(3, Is.LessThan(7));//小于
Assert.That(3, Is.LessThanOrEqualTo(7));
Assert.That(3, Is.AtMost(7)); //最多
Assert.That(3, Is.LessThanOrEqualTo(3));
Assert.That(3, Is.AtMost(3));
Expect( 7, GreaterThan(3)); // Using Inheritance
Expect( 3, AtMost(7));
5、Type Constraint
用于测试对象类型;
Assert.That("Hello", Is.TypeOf(typeof(string))); //当前对象类型是否为了期望类型 Assert.That("Hello", Is.Not.TypeOf(typeof(int))); Assert.That("Hello", Is.InstanceOfType(typeof(string))); //当前对象类型是否为期望类型的实例 Assert.That(5, Is.Not.InstanceOfType(typeof(string))); Assert.That( "Hello", Is.AssignableFrom(typeof(string)));//是否为期望类型所指定的类型 Assert.That( 5, Is.Not.AssignableFrom(typeof(string))); // Using inheritance Expect( 5, Not.InstanceOfType(typeof(string))); Expect( "Hello", AssignableFrom(typeOf(string)));
6、String Constraint 字符串约束;如:string phrase = "Make your tests fail before passing!" Assert.That( phrase, Text.Contains( "tests fail" ) ); Assert.That( phrase, Text.Contains( "make" ).IgnoreCase ); Assert.That( phrase, Text.StartsWith( "Make" ) ); Assert.That( phrase, Text.Not.StartsWith( "Break" ) ); Assert.That( phrase, Text.DoesNotStartWith( "Break" ) ); Assert.That( phrase, Text.EndsWith( "!" ) ); Assert.That( phrase, Text.EndsWith( "PASSING!" ).IgnoreCase ); Assert.That( phrase, Text.Matches( "Make.*tests.*pass" ) ); Assert.That( phrase, Text.Not.Matches( "your.*passing.*tests" ) ); Assert.That( phrase, Text.DoesNotMatch( "your.*passing.*tests" ) ); // Using Inheritance Expect( phrase, Contains( "make" ).IgnoreCase ); Expect( phrase, EndsWith( "!" ) ); Expect( phrase, Matches( "Make.*pass" ) );7、Collection Constraint
集合结束为指定的集合执行测试,主要的语法有:
Is.All...,Has.All... 为集合中的每一个元素申请一个约束,所有元素符合要求则测试成功。
Has.Some... 为集合中的每一个元素申请约束,最少有一个符合要求则测试成功。
Has.None... 所有的元素都不都不符合要求则测试成功。
Is.Unique 测试集合拥有一个唯一值
Has.Member(object) 测试集合拥有一个对象
Is.EquivalentTo(IEnumerable) 测试两个集合相等,需要拥有相同的items.
Is.SubsetOf(IEnumerable) 测试一个集合是否是另一个的子集
int[] iarray = new int[] { 1, 2, 3 };
string[] sarray = new string[] { "a", "b", "c" };
Assert.That( iarray, Is.All.Not.Null );
Assert.That( sarray, Is.All.InstanceOfType(typeof(string)) );
Assert.That( iarray, Is.All.GreaterThan(0) );
Assert.That( sarray, Is.Unique );
Assert.That( iarray, List.Contains(3) );
Assert.That( sarray, List.Contains("b") );
Assert.That( sarray, List.Not.Contains("x") );
Assert.That( new string[] { "c", "a", "b" }, Is.EquivalentTo( sarray ) );
Assert.That( new int[] { 1, 2, 2 }, Is.Not.EquivalentTo( iarray ) );
Assert.That( new int[] { 1, 3 }, Is.SubsetOf( iarray ) );
// Using inheritance
Expect( iarray, All.GreaterThan( 0 ) );
Expect( sarray, Contains("b") );
Expect( new int[] { 1, 3 }, SubsetOf( iarray ) );8、Property Constraint
测试对象的属性。Has.Property(string) //测试指定对象是否存在
Has.Property(string,object) //测试对象属性值是否与提供的值相等
Has.Lenght(int) // 测试对象的长度是否与指定的值相等
Has.Count(int) //测试对象的总数是否与指定的值相等
ArrayList arrayList1 = new ArrayList();
arrayList1.Add("ccnu");
arrayList1.Add("whu");
arrayList1.Add("hsut");
int[] array = new int[8] {0,1,2,3,4,5,6,7 };
NUnit.Framework.Assert.That(array,Has.Property ("Length"));
NUnit.Framework.Assert.That(array,Has.Property ("Length").EqualTo(8));
NUnit.Framework.Assert.That(array,Has.Length (8));
//Array has not Property Count
//NUnit.Framework.Assert.That(array,Has.Count (8)); // Fail
NUnit.Framework.Assert.That(arrayList1,Has .Property ("Count"));
NUnit.Framework.Assert.That(arrayList1,Has.Count(3));