代码改变世界

[翻译]NUnit---Category and Combinatorial Attributes(九)

2013-05-24 22:29  Max蚊子  阅读(917)  评论(0编辑  收藏  举报

Category特性为suites的测试提供另外一个可替换功能。个别测试用例或者fixtures可能被识别为一个特殊的类别。GUI和控制台程序都允许在运行的测试中包含或者排除指定一系列category。当使用了category是,只有选定等等测试才会执行。未选中的测试则根本不会报告。

这个功能在控制台中使用/include and /exclude参数,在GUI中使用单独的Categories选项卡来实现。GUI提供了可视化操作用于随时选择Categories。、

Test Fixture Syntax

namespace NUnit.Tests
{
  using System;
  using NUnit.Framework;

  [TestFixture]
  [Category("LongRunning")]
  public class LongRunningTests
  {
    // ...
  }
}
View Code

Test Syntax

namespace NUnit.Tests
{
  using System;
  using NUnit.Framework;

  [TestFixture]
  public class SuccessTests
  {
    [Test]
    [Category("Long")]
    public void VeryLongTest()
    { /* ... */ }
}
View Code

Custom Category Attributes

从NUnit2.4开始,可以通过继承CategoryAttribute来实现能够被NUnit识别自定义特性。CategoryAttribute默认的protected构造函数设置类名为category名。

下面的示例创建了一个category测试。就和其他category一样,单有一个简单的语法。一个测试报告系统可能会使用这个特性来提供特殊报告。

[AttributeUsage(AttributeTargets.Method, AllowMultiple=false)]
public class CriticalAttribute : CategoryAttribute { }

...

[Test, Critical]
public void MyTest()
{ /*...*/ }
View Code

 

CombinatorialAttribute (NUnit 2.5)

在一个测试中,CombinatorialAttribute更具提供的数据参数指定NUnit产生的可能的测试用例。由于是默认的,故这个特性是可选的。

Example

The following test will be executed six times, as follows:

	MyTest(1, "A")
	MyTest(1, "B")
	MyTest(2, "A")
	MyTest(2, "B")
	MyTest(3, "A")
	MyTest(3, "B")
[Test, Combinatorial]
public void MyTest(
    [Values(1,2,3)] int x,
    [Values("A","B")] string s)
{
    ...
}
View Code

 

See also...

 

网站:feiger.cn         飞鸽博客,关注互联网、站长圈的程序员博客!