條件:安裝NUnit,默認(rèn)安裝即可
第1步:創(chuàng)建一個(gè)class,增加一個(gè)NUnit框架引用到工程
在Microsoft Visual Studio .NET里創(chuàng)建這個(gè)例子時(shí),你需要增加一個(gè)NUnit.framework.dll引用,
具體為:
1、 右鍵點(diǎn)擊reference->add reference->選擇nunit.framework,
2、 然后在webTest文件中添加一個(gè)引用:using NUnit.Framework。當(dāng)然,添加引用的前提是已經(jīng)安裝了NUnit了
3、在工程中創(chuàng)建一個(gè)App.config,如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="NUnit">
<section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<NUnit>
<TestRunner>
<!-- Valid values are STA,MTA. Others ignored. -->
<add key="ApartmentState" value="STA" />
</TestRunner>
</NUnit>
</configuration>
第2步.為工程加一個(gè)類.
為工程加一個(gè)NumbersFixture類。這里是這個(gè)例子的代碼。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace number
{
[TestFixture]
public class Numbertest
{
[Test]
public void AddOneNumber()
{
int a = 1;
int b = 2;
int sum = a + b;
Assert.AreEqual(sum, 3);
Console.WriteLine(sum);
}
}
}
[TestFixture]是NUnit的一個(gè)重要的屬性,意思是這個(gè)類包含了測(cè)試代碼。
[Test]是需要測(cè)試的方法
第3步:用NUnit運(yùn)行
直接打開(kāi)NUnit運(yùn)行該dll文件或者建立你的Visual Studio工程,運(yùn)用NUnit測(cè)試
一、直接打開(kāi)NUnit運(yùn)行該dll文件
打開(kāi)NUnit,點(diǎn)擊file->open,選擇剛剛編譯的dll文件,這樣把文件加載到NUnit了,在它的界面的左邊,我們可以看到剛才編寫(xiě)的測(cè)試函數(shù)AddOneNumber,選擇工程項(xiàng)目,或是文件,或是測(cè)試函數(shù),然后點(diǎn)擊界面右邊的Run,如果一路綠燈,OK,測(cè)試通過(guò)。
配置WAtin和NUnit一起使用,框架配置如下:
按上面步驟配置NUnit后,配置Watin
1:在工程中,References處右鍵選擇Add Reference,Browse選擇WatiN安裝目錄" WatiN-2.0.20.1089-net-2.0in"下的WatiN.Core.dll
2:添加一個(gè)引用:usingWatiN.Core; 如圖
此時(shí)完成配置了。可以用NUnit當(dāng)控制器運(yùn)行watin腳本了。