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 MessageNUnit2.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()
{ /* ... */ }
}
}
作者: Leo_wl
出处: http://HdhCmsTestcnblogs测试数据/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息查看更多关于NUnitDescription and Exception Attributes11的详细内容...