zoukankan      html  css  js  c++  java
  • [翻译]NUnitException && Utility Methods (六)

    Exception Asserts (NUnit 2.5)

    Assert.Throws 作用于类本身,而不是比较值,它调用代理来验证抛代码片段出一个特定异常。

    在一个类可中Assert成功时可以是返回一个Exception而不是void,下面的例子代表了几种使用方式。

    Assert.Throws可以使用约束参数或者是异常类型作为预期的抛出异常类型。类型格式可以是泛型或者一般类型。

    Assert.DoesNotThrow用于确认代理不会抛出异常。

    Assert.Catch 与 Assert.Throws 相似,但是当一个异常派生意另一个异常是会通过。

    View Code
    Exception Assert.Throws( Type expectedExceptionType, TestDelegate code );
    Exception Assert.Throws( Type expectedExceptionType, TestDelegate code, 
            string message );
    Exception Assert.Throws( Type expectedExceptionType, TestDelegate code, 
            string message, params object[] parms);
    
    Exception Assert.Throws( IResolveConstraint constraint, TestDelegate code );
    Exception Assert.Throws( IResolveConstraint constraint, TestDelegate code, 
            string message );
    Exception Assert.Throws( IResolveConstraint constraint, TestDelegate code, 
            string message, params object[] parms);
    
    T Assert.Throws<T>( TestDelegate code );
    T Assert.Throws<T>( TestDelegate code, string message );
    T Assert.Throws<T>( TestDelegate code, string message, 
            params object[] parms);
            
    void Assert.DoesNotThrow( TestDelegate code );
    void Assert.DoesNotThrow( TestDelegate code, string message );
    void Assert.DoesNotThrow( TestDelegate code, string message, 
            params object[] parms);
    
    Exception Assert.Catch( TestDelegate code );
    Exception Assert.Catch( TestDelegate code, string message );
    Exception Assert.Catch( TestDelegate code, string message, 
            params object[] parms);
    
    Exception Assert.Catch( Type expectedExceptionType, TestDelegate code );
    Exception Assert.Catch( Type expectedExceptionType, TestDelegate code, 
            string message );
    Exception Assert.Catch( Type expectedExceptionType, TestDelegate code, 
            string message, params object[] parms);
    
    T Assert.Catch<T>( TestDelegate code );
    T Assert.Catch<T>( TestDelegate code, string message );
    T Assert.Catch<T>( TestDelegate code, string message, 
            params object[] parms);

    在上面的例子中TestDelegate void TestDelegate()的一个代理类型,用于执行验证问题的代码。在.NET2.0中可以使用匿名代理。如果在C#3.0中可能会是一个lambda表达式。

    下面的例子显示了统一例子的几个方式:

    View Code
    [TestFixture]
    public class AssertThrowsTests
    {
      [Test]
      public void Tests()
      {
        // .NET 1.x
        Assert.Throws( typeof(ArgumentException),
          new TestDelegate(MethodThatThrows) );
          
        // .NET 2.0
        Assert.Throws<ArgumentException>( MethodThatThrows() );
    
        Assert.Throws<ArgumentException>(
          delegate { throw new ArgumentException(); } );
    
        // Using C# 3.0      
        Assert.Throws<ArgumentException>(
          () => throw new ArgumentException(); } );
      }
      
      void MethodThatThrows()
      {
        throw new ArgumentException();
      }

    下面代码演示了使用返回值来验证异常:

    View Code
    [TestFixture]
    public class UsingReturnValue
    {
      [Test]
      public void TestException()
      {
        MyException ex = Assert.Throws<MyException>(
          delegate { throw new MyException( "message", 42 ); } );
        Assert.That( ex.Message, Is.EqualTo( "message" ) );
        Assert.That( ex.MyParam, Is.EqualTo( 42 ) ); 
      }

    下面的例子使用包含了约束的重载来验证异常:

    View Code
    [TestFixture]
    public class UsingConstraint
    {
      [Test]
      public void TestException()
      {
        Assert.Throws( Is.Typeof<MyException>()
                     .And.Message.EqualTo( "message" )
                     .And.Property( "MyParam" ).EqualTo( 42 ),
          delegate { throw new MyException( "message", 42 ); } );
      }


    使用适合自己的风格来撰写代码。

    Exact Versus Derived Types

    当使用Type作为参数是,Assert.Throws 需要抛出精确的类型。如果希望测试派生Type,使用以下允许指定约束的格式。可以二选一使用允许派生类型的Assert.Throws或者Assert.Catch。看如下例子:

    View Code
    // Require an ApplicationException - derived types fail!
    Assert.Throws( typeof(ApplicationException), code );
    Assert.Throws<ApplicationException>()( code );
    
    // Allow both ApplicationException and any derived type
    Assert.Throws( Is.InstanceOf( typeof(ApplicationException), code );
    Assert.Throws( Is.InstanceOf<ApplicationException>(), code );
    
    // Allow both ApplicationException and any derived type
    Assert.Catch<ApplicationException>( code );
    
    // Allow any kind of exception
    Assert.Catch( code );

    See also...

    Utility Methods

    4个功能方法:Pass(), Fail(), Ignore() and Inconclusive() 用来直接控制测试进程:

    View Code
    Assert.Pass();
    Assert.Pass( string message );
    Assert.Pass( string message, object[] parms );
    
    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.Inconclusive();
    Assert.Inconclusive( string message );
    Assert.Inconclusive( string message, object[] parms );

     Assert.Pass方法可以直接结束测试,并标记为成功。这会抛出一个异常,能更方便的从测试返回。然而, Assert.Pass允许在测试结果中记录一个信息并且让测试在某些情况下更容易阅读。

    另外,和本页面其他方法一样,能够从嵌套的方法调用并立即终止运行的测试。

    Assert.Fail方法提供生成一个失败,这个失败必须基于未被其他方法封装。这在开发指定项目assertions的时候非常有用。

    下面是一个示例,它创建了一个私有assertion来测试一个字符串是否包含一个预期值。

    View Code
    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 方法用于在运行时动态忽悠一个测试用例。可以在一个test、setup 、fixture setup 等方法。

    我们建议仅在独立用例中使用。这些提供更广泛的包含或者排除的测试,或者可以在不同的程序集不同的时机分离测试。

    Assert.Inconclusive方法指示使用现有数据测试不能完成。需要其他情况下用另外的数据运行才能完成,生产一个成功或者失败的结果。

    ※如果你觉得这篇文章不错,请点击推荐。如果你觉得我写的对你有用请关注我。
    作者:Max蚊子
    网站:feiger.cn         飞鸽博客,关注互联网、站长圈的程序员博客!
                 
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Axure案例:用中继器实现便捷好用的3级菜单--转载
    我说CMMI之七:需求管理过程域--转载
    我说CMMI之六:CMMI的评估--转载
    我说CMMI之五:CMMI 4个等级的区别--转载
    我说CMMI之四:CMMI的表示方法--转载
    我说CMMI之三:CMMI的构件--转载
    我说CMMI之二:CMMI里有什么?--转载
    我说CMMI之一:CMMI是什么--转载
    PMBOK :美国的项目管理知识体系
    有关 CMMI
  • 原文地址:https://www.cnblogs.com/kim01/p/2994378.html
Copyright © 2011-2022 走看看