Test2
TearDown
TestFixtureTearDown
Test Attribute簡介
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
{
文章來源于領測軟件測試網 http://www.kjueaiud.com/