[C#]整合VS2010和NUnit
整合VS2010和NUnit
羅朝輝 (http://www.rzrgm.cn/kesalin/)
軟件下載
.Net單元測(cè)試工具 NUnit下載:http://www.nunit.org/index.php?p=download,最新的為NUnit-2.6.0.12051.msi,下載安裝。
VS2010 NUnit 整合插件 Visual Nunit 2010下載:http://visualstudiogallery.msdn.microsoft.com/c8164c71-0836-4471-80ce-633383031099,下載安裝完畢就能在 VS2010 的 view->Other Windows中看到 Visual Nunit了(或使用快捷鍵Ctrl + F7),打開(kāi)該視圖,將之拖到合適的位置。
測(cè)試程序
1,新建名為 NUnitSample 的C#控制臺(tái)應(yīng)用程序。在References中增加References:nunit.framework,路徑默認(rèn)為:C:\Program Files \NUnit 2.6\bin\framework\nunit.framewor.dll。
2,刪除 Class1.cs,增加兩個(gè)C#文件:NUnitSample.cs 和NUnitSampleTest.cs。其內(nèi)容分別為:
// File: NUnitSample.cs
//
using System;
namespace NUnitSample
{
public class MathUtil
{
public int Add(int a, int b)
{
return a + b;
}
static void Main(string[] args)
{
}
}
}
和
// File: NUnitSampleTest.cs
//
using System;
using NUnit.Framework;
namespace NUnitSample
{
[TestFixture]
public class NUnitSampleTest
{
[Test]
public void AddTest()
{
MathUtil util = new MathUtil();
int result = util.Add(4, 4);
Assert.AreEqual(8, result);
}
[Test]
public void AddTestFailure()
{
MathUtil util = new MathUtil();
int result = util.Add(4, 4);
Assert.AreEqual(6, result);
}
}
}
在上面的代碼中引用 NUnit.Framework,使用[TestFixture]表明這是用于測(cè)試的類(lèi),在其中使用 [Test]表示具體的測(cè)試用例,在這里添加了兩個(gè)測(cè)試用例:一個(gè)成功的和一個(gè)失敗的測(cè)試。
3,編譯,然后點(diǎn)擊 Visula Unit中的 Run 按鈕就能運(yùn)行測(cè)試用例了。從下圖可以看到失敗的測(cè)試用例顯示的比較詳細(xì)的信息。

浙公網(wǎng)安備 33010602011771號(hào)