您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > Nunit
在.NET環(huán)境中使用單元測試工具Nunit
作者:網絡轉載 發(fā)布時間:[ 2013/4/8 15:59:45 ] 推薦標簽:

  Test Attribute簡介

  Test attribute主要用來標示在text fixture中的method,表示這個method需要被Test Runner application所執(zhí)行。有Test attribute的method必須是public的,并且必須return void,也沒有任何傳入的參數。如果沒有符合這些規(guī)定,在Test Runner GUI之中是不會列出這個method的,而且在執(zhí)行Unit Test的時候也不會執(zhí)行這個method。上面的程序代碼示范了使用這個attribute的方法。

  SetUp 和 Teardown Attributes簡介

  在寫Unit Tests的時候,有時你會需要在執(zhí)行每一個test method之前(或之后)先作一些預備或善后工作。當然,你可以寫一個private的method,然后在每一個test method的一開頭或末端呼叫這個特別的method;蛘撸憧梢允褂梦覀円榻B的SetUp及Teardown Attributes來達到相同的目的。如同這兩個Attributes的名字的意思,有Setup Attribute的method會在該TextFixture中的每一個test method被執(zhí)行之前先被Test Runner所執(zhí)行,而有Teardown Attribute的method則會在每一個test method被執(zhí)行之后被Test Runner所執(zhí)行。一般來說,Setup Attribute及Teardown Attribute被用來預備一些必須的objects(對象),例如database connection、等等。上面的程序代碼示范了使用這個attribute的方法。

  ExpectedException Attributes簡介

  有的時候,你希望你的程序在某些特殊的條件下會產生一些特定的exception。要用Unit Test來測試程序是否如預期的產生exception,你可以用一個try..catch的程序區(qū)段來catch(捕捉)這個exception,然后再設一個boolean的值來證明exception的確發(fā)生了。這個方法固然可行,但是太花費功夫。事實上,你應該使用這個 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

  }

  }

  }

  如果上面的程序被執(zhí)行的時候,如果一旦exception發(fā)生,而且這個exception的type(類型信息)是 InvalidOperationException 的話,這個test會順利通過驗證。如果你預期你的程序代碼會產生多個exception的話,你也可以一次使用多個 ExpectedException attribute。但是,一個test method應該只測試一件事情,一次測試多個功能是不好的做法,你應該盡量避免之。另外,這個attributes并不會檢查inheirtance的關系,也是說,如果你的程序代碼產生的exception是繼承自InvalidOperationException 的subclass(子類化)的話,這個test執(zhí)行的時候將不會通過驗證。簡而言之,當你使用這個attribute的時候,你要明確的指明所預期的 exception是哪個type(類型信息)的。

  Ignore Attributes簡介

  這個attribute你大概不會經常用的,但是一旦需要的時候,這個attribute是很方便使用的。你可以使用這個attribute來標示某個test method,叫Test Runner在執(zhí)行的時候,略過這個method不要執(zhí)行。使用這個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的執(zhí)行結果里面,也會提醒你這個被略過的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();

上一頁1234下一頁
軟件測試工具 | 聯系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd