NUnit使用详解(二)

时间:2022-12-13 08:04:28

转载:http://hi.baidu.com/grayworm/item/39aa11c5d9375d56bdef6990

五:常用断言
在NUnit中,断言是单元测试的核心。NUnit提供了一组丰富的断言,这些断言是Assert类的静态方法。如果一个断言失败,方法的调用不会返回值,并且会报告一个错误。如果一个测试包含多个断言,那些紧跟失败断言的断言都不会执行,因此,通常每个测试方法最好只有一个断言。

Assert类提供了最常用的断言。我们将Assert方法按如下分组:
    a.同等(Equality)断言 
    b.一致性(Identity)断言 
    c.比较(Comparison)断言 
    d.类型(Type)断言 
    e.条件(Condition)测试 
    f.工具(Utility)方法 
    
1.同等断言
    主要包括Assert.AreEqual()、Assert.AreNotEqual()Assert.IsNaN()
    前两个方法测试2个参数是否相等。重载的方法支持普通的值类型。
    Assert.AreEqual( int expected, int actual );
    Assert.AreEqual( int expected, int actual, string message );
    Assert.AreEqual( int expected, int actual, string message,params object[] parms );
         
    Assert.AreEqual( uint expected, uint actual );
    Assert.AreEqual( uint expected, uint actual, string message );
    Assert.AreEqual( uint expected, uint actual, string message,params object[] parms );

    Assert.AreEqual( decimal expected, decimal actual );
    Assert.AreEqual( decimal expected, decimal actual, string message );
    Assert.AreEqual( decimal expected, decimal actual, string message,params object[] parms );

    Assert.AreEqual( float expected, float actual, float tolerance );
    Assert.AreEqual( float expected, float actual, float tolerance,string message );
    Assert.AreEqual( float expected, float actual, float tolerance,string message, params object[] parms );

    Assert.AreEqual( double expected, double actual, double tolerance );
    Assert.AreEqual( double expected, double actual, double tolerance,string message );
    Assert.AreEqual( double expected, double actual, double tolerance,string message, params object[] parms );

    Assert.AreEqual( object expected, object actual );
    Assert.AreEqual( object expected, object actual, string message );
    Assert.AreEqual( object expected, object actual, string message,params object[] parms );

    Assert.AreNotEqual( int expected, int actual );
    Assert.AreNotEqual( int expected, int actual, string message );
    Assert.AreNotEqual( int expected, int actual, string message,params object[] parms );

    Assert.AreNotEqual( uint expected, uint actual );
    Assert.AreNotEqual( uint expected, uint actual, string message );
    Assert.AreNotEqual( uint expected, uint actual, string message,params object[] parms );

    Assert.AreNotEqual( decimal expected, decimal actual );
    Assert.AreNotEqual( decimal expected, decimal actual, string message );
    Assert.AreNotEqual( decimal expected, decimal actual, string message,params object[] parms );

    Assert.AreNotEqual( float expected, float actual );
    Assert.AreNotEqual( float expected, float actual, string message );
    Assert.AreNotEqual( float expected, float actual, string message,params object[] parms );

    Assert.AreNotEqual( double expected, double actual );
    Assert.AreNotEqual( double expected, double actual, string message );
    Assert.AreNotEqual( double expected, double actual, string message,params object[] parms );

    Assert.AreNotEqual( object expected, object actual );
    Assert.AreNotEqual( object expected, object actual, string message );
    Assert.AreNotEqual( object expected, object actual, string message,params object[] parms );

    不同类型的数值可以按期望的那样进行比较。下面的断言会成功: 
        Assert.AreEqual( 5, 5.0 );
    float型和double型的数值通常使用一个附加参数来进行比较,这个参数代表一个误差,在这个误差范围内,它们视为相等。

    如果2个一维数组有相同的长度,而且相应的数组元素也相等,那么通过调用Assert.AreEqual方法,这2个数组视为相等。
        注: 多维数组,嵌套数组(数组的数组),以及其他集合类型,例如ArrayList目前还不支持。

2.一致性断言
    Assert.AreSame()方法、Assert.AreNotSame方法。这两个方法主要判断两个参数引用的是否是同一个对象。
        Assert.AreSame( object expected, object actual );
        Assert.AreSame( object expected, object actual, string message );
        Assert.AreSame( object expected, object actual, string message,params object[] parms );

        Assert.AreNotSame( object expected, object actual );
        Assert.AreNotSame( object expected, object actual, string message );
        Assert.AreNotSame( object expected, object actual, string message,params object[] parms );
    
    Assert.Contains()方法用来测试在一个数组或列表里是否包含该对象。
       Assert.Contains( object anObject, IList collection );
        Assert.Contains( object anObject, IList collection,string message );
        Assert.Contains( object anObject, IList collection,string message, params object[] parms );
    
3.比较断言
    Assert.Greater():测试一个对象是否大于另外一个。 
    Assert.Less():测试一个对象是否于小另外一个。 
    
    Assert.Greater( int arg1, int arg2 );
    Assert.Greater( int arg1, int arg2, string message );
    Assert.Greater( int arg1, int arg2, string message,object[] parms );

    Assert.Greater( uint arg1, uint arg2 );
    Assert.Greater( uint arg1, uint arg2, string message );
    Assert.Greater( uint arg1, uint arg2, string message,object[] parms );

    Assert.Greater( decimal arg1, decimal arg2 );
    Assert.Greater( decimal arg1, decimal arg2, string message );
    Assert.Greater( decimal arg1, decimal arg2, string message,object[] parms );

    Assert.Greater( double arg1, double arg2 );
    Assert.Greater( double arg1, double arg2, string message );
    Assert.Greater( double arg1, double arg2, string message,object[] parms );

    Assert.Greater( double arg1, double arg2 );
    Assert.Greater( double arg1, double arg2, string message );
    Assert.Greater( double arg1, double arg2, string message,object[] parms );

    Assert.Greater( float arg1, float arg2 );
    Assert.Greater( float arg1, float arg2, string message );
    Assert.Greater( float arg1, float arg2, string message,object[] parms );

    Assert.Greater( IComparable arg1, IComparable arg2 );
    Assert.Greater( IComparable arg1, IComparable arg2, string message );
    Assert.Greater( IComparable arg1, IComparable arg2, string message,object[] parms ); 
    
    Assert.Less( int arg1, int arg2 );
    Assert.Less( int arg1, int arg2, string message );
    Assert.Less( int arg1, int arg2, string message,object[] parms );
        
    Assert.Less( uint arg1, uint arg2 );
    Assert.Less( uint arg1, uint arg2, string message );
    Assert.Less( uint arg1, uint arg2, string message,object[] parms );
        
    Assert.Less( decimal arg1, decimal arg2 );
    Assert.Less( decimal arg1, decimal arg2, string message );
    Assert.Less( decimal arg1, decimal arg2, string message,object[] parms );
        
    Assert.Less( double arg1, double arg2 );
    Assert.Less( double arg1, double arg2, string message );
    Assert.Less( double arg1, double arg2, string message,object[] parms );
        
    Assert.Less( float arg1, float arg2 );
    Assert.Less( float arg1, float arg2, string message );
    Assert.Less( float arg1, float arg2, string message,object[] parms );
        
    Assert.Less( IComparable arg1, IComparable arg2 );
    Assert.Less( IComparable arg1, IComparable arg2, string message );
    Assert.Less( IComparable arg1, IComparable arg2, string message,object[] parms );
    
4.类型断言
    Assert.IsInstanceOfType():判断一个对象的类型是否是期望的类型
    Assert.IsNotInstanceOfType():判断一个对象的类型是否不是期望的类型
    Assert.IsAssignableFrom():判断一个对象的类型是否属于某种类型
    Assert.IsNotAssignableFrom():判断一个对象的类型是否不属于某种类型
        
    Assert.IsInstanceOfType( Type expected, object actual );
    Assert.IsInstanceOfType( Type expected, object actual,string message );
    Assert.IsInstanceOfType( Type expected, object actual,string message, params object[] parms );
        
    Assert.IsNotInstanceOfType( Type expected, object actual );
    Assert.IsNotInstanceOfType( Type expected, object actual,string message );
    Assert.IsNotInstanceOfType( Type expected, object actual,string message, params object[] parms );
       
    Assert.IsAssignableFrom( Type expected, object actual );
    Assert.IsAssignableFrom( Type expected, object actual,string message );
    Assert.IsAssignableFrom( Type expected, object actual,string message, params object[] parms );
        
    Assert.IsNotAssignableFrom( Type expected, object actual );
    Assert.IsNotAssignableFrom( Type expected, object actual,string message );
    Assert.IsNotAssignableFrom( Type expected, object actual,string message, params object[] parms );
    
5.条件测试断言
这些方法测试并把测试的值作为他们的第一个参数以及把一个消息作为第二个参数,第二个参数是可选的。本文提供了下面的方法:
    Assert.IsTrue( bool condition );
    Assert.IsTrue( bool condition, string message );
    Assert.IsTrue( bool condition, string message, object[] parms );

    Assert.IsFalse( bool condition);
    Assert.IsFalse( bool condition, string message );
    Assert.IsFalse( bool condition, string message, object[] parms );

    Assert.IsNull( object anObject );
    Assert.IsNull( object anObject, string message );
    Assert.IsNull( object anObject, string message, object[] parms );

    Assert.IsNotNull( object anObject );
    Assert.IsNotNull( object anObject, string message );
    Assert.IsNotNull( object anObject, string message, object[] parms );

    Assert.IsNaN( double aDouble );
    Assert.IsNaN( double aDouble, string message );
    Assert.IsNaN( double aDouble, string message, object[] parms );

    Assert.IsEmpty( string aString );
    Assert.IsEmpty( string aString, string message );
    Assert.IsEmpty( string aString, string message,params object[] args );

    Assert.IsNotEmpty( string aString );
    Assert.IsNotEmpty( string aString, string message );
    Assert.IsNotEmpty( string aString, string message,params object[] args );

    Assert.IsEmpty( ICollection collection );
    Assert.IsEmpty( ICollection collection, string message );
    Assert.IsEmpty( ICollection collection, string message,params object[] args );

    Assert.IsNotEmpty( ICollection collection );
    Assert.IsNotEmpty( ICollection collection, string message );
    Assert.IsNotEmpty( ICollection collection, string message,params object[] args );
    
6.StringAssert断言

    StringAssert.Contains( string expected, string actual );
    StringAssert.Contains( string expected, string actual,string message );
    StringAssert.Contains( string expected, string actual,string message, params object[] args );

    StringAssert.StartsWith( string expected, string actual );
    StringAssert.StartsWith( string expected, string actual, string message );
    StringAssert.StartsWith( string expected, string actual,string message, params object[] args );

    StringAssert.EndsWith( string expected, string actual );
    StringAssert.EndsWith( string expected, string actual,string message );
    StringAssert.EndsWith( string expected, string actual,string message, params object[] args );

    StringAssert.AreEqualIgnoringCase( string expected, string actual );
    StringAssert.AreEqualIgnoringCase( string expected, string actual,string message );
    StringAssert.AreEqualIgnoringCase( string expected, string actual,string message params object[] args );
    
7.实用方法
   Fail()和Ignore(),是为了让我们对测试过程有更多的控制:
        Assert.Fail();
        Assert.Fail( string message );
        Assert.Fail( string message, object[] parms );

        Assert.Ignore();
        Assert.Ignore( string message );
        Assert.Ignore( string message, object[] parms );
    Assert.Fail方法为你提供了创建一个失败测试的能力:
        public void AssertStringContains( string expected, string actual )
        {
            AssertStringContains( expected, actual, string.Empty );
        }

        public void AssertStringContains( string expected, string actual,string message )
        {
            if ( actual.IndexOf( expected ) < 0 )
                Assert.Fail( message );
        }
   Assert.Ignore方法为你提供在运行时动态忽略一个测试或者一个测试套件(suite)的能力。它可以在一个测试,一个setup或fixture setup的方法中调用。我们建议你只在无效的案例中使用。它也为更多扩展的测试包含或排斥提供了目录能力,或者你可以简单将不同情况下运行的测试运行分解到不同的程序集。