• NUnitDescription and Exception Attributes11


    NUnit---Description and Exception Attributes(十一)

    Description (NUnit 2.4)

    Description特性给Test, TestFixture or Assembly应用一个描述性文字。这些文字会显示在输出的XML文档中,在Test Property对话框也会显示。

    Example:

     View Code
    [assembly: Description("Assembly description here")]
    
    namespace NUnit.Tests
    {
      using System;
      using NUnit.Framework;
    
      [TestFixture, Description("Fixture description here")]
      public class SomeTests
      {
        [Test, Description("Test description here")] 
        public void OneTest()
        { /* ... */ }
      }
    }

    Note:Test and TestFixture特性支持一个可选的Description属性。Description特性应该使用到新的应用程序。如果同时使用,Description特性优先级高。

    ExpectedExceptionAttribute (NUnit 2.0 plus Updates)

    可以使用这种方式来指定一个测试用例抛出期望的异常。这个特性有一些列定位和命名参数,我们会根据它的目标来分开讨论。

    Specifying the Expected Exception Type

    初始特性是在NUnit2.0中引入,使用一个参数给出期望的精确的异常类型。例如:

    [ExpectedException( typeof( ArgumentException ) )]
    public void TestMethod()
    {
    ...

    从NUnit2.2.4开始,可以使用字符串的异常类型,避免定义程序集的引用。

    [ExpectedException( "System.ArgumentException" ) )]
    public void TestMethod()
    {
    ...

    以上两种例子实现相同的功能,只有抛出System.Argument异常的测试用例执行成功。

    Specifying the Expected Message

    NUnit2.1引入了一个包含两个参数的构造函数,指定异常的message属性文本。NUnit2.2.4之后,相同的扩展使用一个字符串参数到构造函数。在NUnit2.4中,这些参数标记为已过时,并提供一个命名参数来替代。

    复制代码
    // Obsolete form:
    [ExpectedException( typeof( ArgumentException ), "expected message" )]
    [ExpectedException( "System.ArgumentException", "expected message" )]
    
    // Prefered form:
    [ExpectedException( typeof( ArgumentException ), ExpectedMessage="expected message" )]
    [ExpectedException( "System.ArgumentException", ExpectedMessage="expected message" )]
    复制代码

    在NUnit2.4,除了精确的匹配还可以指定异常信息附加额外的测试,通过使用枚举类型的MatchType参数来实现,示例如下:

    复制代码
    public enum MessageMatch
    {
        /// Expect an exact match
        Exact,    
        /// Expect a message containing the parameter string
        Contains,
        /// Match the regular expression provided as a parameter
        Regex,
        /// Expect a message starting with the parameter string
        StartsWith
    }
    复制代码

    如果没有指定MatchType,会使用精确的匹配。

    Specifying a Custom Error Message

    在NUnit2.4,如果ExpectedException消息不满足则可以指定一个自定义消息,使用UserMessage参数:

    [ExpectedException( typeof( ArgumentException ), UserMessage="Custom message" )]
    public void TestMethod()
    {
    ...

    Handling the Exception in Code
     

    如果需要处理的异常太复杂,通常的做法是在测试代码中使用try/catch快来处理。作为替代,NUnit2.4允许调用一个方法来处理异常。在需要用相同的方式处理多个异常时特别有效。

    通常可以使用IExpectException接口来实现异常处理,示例如下:

    public interface IExpectException
    {
        void HandleException( System.Exception ex );
    }

    只有通过标记为ExpectedException来调用异常处理程序。如果代码中的异常类型等所有检查都通过,特性可以不指定任何参数就指出期望的异常。

    一个handler可以使用Handler参数指定给特定的方法

    复制代码
    [ExpectedException( Handler="HandlerMethod" )]
    public void TestMethod()
    {
    ...
    }
    
    public void HandlerMethod( System.Exception ex )
    {
    ...
    }
    复制代码

    这个技巧可以不实现IExpectException或者混合使用就可以使用。在后面的例子中,指定处理程序可以应用到任何指定了的方法,而一般的异常处理程序适用于指定了ExpectedException的其他方法。

    指定了之后,处理程序会坚持异常,并且断言相关的属性。在测试中的任何的失败结果信息会与其他断言保持一致的格式。

    ExpectedException

    复制代码
    namespace NUnit.Tests
    {
      using System;
      using NUnit.Framework;
    
      [TestFixture]
      public class SuccessTests
      {
        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void ExpectAnExceptionByType()
        { /* ... */ }
    
        [Test]
        [ExpectedException("System.InvalidOperationException")]
        public void ExpectAnExceptionByName()
        { /* ... */ }
      }
    }
    复制代码
  • 相关阅读:
    jQuery 折叠,自动完成,小提示,树,验证插件(bassistance.de)
    多样化的连结提示效果(Tips)
    Ext开源 Extjs2.0 人力资源管理(ASP.NET)
    JavaScript面向对象编程
    访问Ext.data.store的数据
    Ext核心代码分析之Function.createDelegate
    支持firefox的省略符
    Ext 2.0下Jquery的整合使用示例
    多样化的垂直菜单(OUTLOOK菜单)
    使用 jQuery 简化 Ajax 开发
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3123023.html
Copyright © 2020-2023  润新知