Test attribute主要用来标示在text fixture中的method,表示这个method需要被Test Runner application所执行。有Test attribute的method必须是public的,并且必须return void,也没有任何传入的参数。如果没有符合这些规定,在Test Runner GUI之中是不会列出这个method的,而且在执行Unit Test的时候也不会执行这个method.上面的程序代码示范了使用这个attribute的方法。
SetUp 和 Teardown Attributes简介
在写Unit Tests的时候,有时你会需要在执行每一个test method之前(或之后)先作一些预备或善后工作。当然,你可以写一个private的method,然后在每一个test method的一开头或最末端呼叫这个特别的method.或者,你可以使用我们要介绍的SetUp及Teardown Attributes来达到相同的目的。如同这两个Attributes的名字的意思,有Setup Attribute的method会在该TextFixture中的每一个test method被执行之前先被Test Runner所执行,而有Teardown Attribute的method则会在每一个test method被执行之后被Test Runner所执行。一般来说,Setup Attribute及Teardown Attribute被用来预备一些必须的objects(对象),例如database connection、等等。上面的程序代码示范了使用这个attribute的方法。
ExpectedException Attributes简介
有的时候,你希望你的程序在某些特殊的条件下会产生一些特定的exception.要用Unit Test来测试程序是否如预期的产生exception,你可以用一个try……catch的程序区段来catch(捕捉)这个exception,然后再设一个boolean的值来证明exception的确发生了。这个方法固然可行,但是太花费功夫。事实上,你应该使用这个ExpectedException attribute来标示某个method应该产生哪一个exception,如同下面的范例所示:
namespace UnitTestingExamples
{
using System;
using NUnit.Framework;
[TestFixture]
public class SomeTests
{
[Test]
[ExpectedException(typeof(InvalidOperationException))]
public void Test1()
{
// Do something that throws an InvalidOperationException
}
}
}
如果上面的程序被执行的时候,如果一旦exception发生,而且这个exception的type(类型信息)是InvalidOperationException 的话,这个test就会顺利通过验证。如果你预期你的程序代码会产生多个exception的话,你也可以一次使用多个ExpectedException attribute。但是,一个test method应该只测试一件事情,一次测试多个功能是不好的做法,你应该尽量避免之。另外,这个attributes并不会检查inheirtance的关系,也就是说,如果你的程序代码产生的exception是继承自InvalidOperationException 的subclass(子类化)的话,这个test执行的时候将不会通过验证。简而言之,当你使用这个attribute的时候,你要明确的指明所预期的exception是哪个type(类型信息)的。
Ignore Attributes简介
这个attribute你大概不会经常用的,但是一旦需要的时候,这个attribute是很方便使用的。你可以使用这个attribute来标示某个test method,叫Test Runner在执行的时候,略过这个method不要执行。使用这个Ignore attribute的方法如下:
namespace UnitTestingExamples
{
using System;
using NUnit.Framework;
[TestFixture]
public class SomeTests
{
[Test]
[Ignore("We're skipping this one for now.")]
public void TestOne()
{
// Do something...
}
}
}
如果你想要暂时性的comment out一个test method的话,你应该考虑使用这个attribute。这个attribute让你保留你的test method,在Test Runner的执行结果里面,也会提醒你这个被略过的test method的存在。
NUnit Assert Class简介
除了以上所提到的这些用来标示测试程序所在的attributes之外,NUnit还有一个重要的class你应该要知道如何使用。这个class就是Assert class。Assert class提供了一系列的static methods,让你可以用来验证主要程序的结果与你所预期的是否一样。Assert class代替了旧的Assertion class,下面是这个类的方法:
Assert.IsTrue( bool );
Assert.IsFalse( bool );
Assert.IsNull( bool );
Assert.IsNotNull( bool );
Assert.AreSame( object, object )
Assert.AreEqual( object, object );
Assert.AreEqual( int, int );
Assert.AreEqual( float, float, float );
Assert.AreEqual( double, double, double );
Assert.Fail();
使用这个类的示例如下:
namespace UnitTestingExamples
{
using System;
using NUnit.Framework;
[TestFixture]
public class SomeTests
{
[Test]
public void TestEventLengthString()
{
// Should return true
bool bResult1 = Class1.CheckPalindrome("ABCCBA");
Assert.IsTrue(bResult1);
// Should return false
bool bResult2 = Class1.CheckPalindrome("ABCDBA");
Assert.IsFalse(bResult2);
}
[Test]
public void TestOddLengthString()
{
//should return true;
Assert.IsTrue(Class1.CheckPalindrome("ABCDCBA"));
// Should return false
Assert.IsFalse(Class1.CheckPalindrome("ABCDEBA"));
}
}
}
执行你的Tests
好,现在我们已经讨论过写Unit Tests的基本步骤及方法,现在让我们来看看如何执行你所写的Unit Tests.事实上非常简单。NUnit里面有两个已经写好的Test Runner applications:一个是窗口GUI程序,一个是console XML(命令列)程序。你可以自由选择你所喜欢的方式,基本上是没有什么差别的。
如果你要使用窗口GUI的Test Runner app,你只需要执行该程序,然后告诉它你要执行的test method所在的assembly位置。这个包含有你所写test methods的assembly是那一个class library(或是executable,*.dll或*.exe) assembly,其中含有前面谈到的Test Fixtures.当你告诉Test Runner你的assembly所在的位置,Test Runner会自动load这个asembly,然后把所有的class及test methods都列在窗口的左栏。当你按下‘Run’按键时,你就会自动执行所有列出来的test methods.你也可以double click其中的一个test class,或是一个test method之上,这样会自动只执行该class或是该method.底下是窗口GUI Test Runner执行时的样子:
在一些的情况下,特别是你想要在你自己写的build script中加入Unit Testing的情况下,你大概不会使用GUI Test Runner.在这个自动执行build script的情况下,你一般会把你build的结果贴在网页,或写入log file里面存作纪录,以供程序开发人员、经理或是客户可以藉由检查这个纪录知道详细情况。在这个情况,你可以用NUnit 2.1的console Test Runner application.这个Test Runner可以传入assembly的位置当参数,其测试执行结果是一个XML字符串。你可以用XSLT或是CSS把这个XML结果转换成HTML,或是其它你想要的格式。如果你需要用到这个功能的话,请查看NUnit文件中有关console Test Runner application的资料。

