您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > Nunit
如何在.NET中應(yīng)用NUnit
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2014/1/3 9:04:30 ] 推薦標簽:NUnit 框架 NET

條件:安裝NUnit,默認安裝即可

第1步:創(chuàng)建一個class,增加一個NUnit框架引用到工程

在Microsoft Visual Studio .NET里創(chuàng)建這個例子時,你需要增加一個NUnit.framework.dll引用,

具體為:

1、 右鍵點擊reference->add reference->選擇nunit.framework,

2、 然后在webTest文件中添加一個引用:using NUnit.Framework。當(dāng)然,添加引用的前提是已經(jīng)安裝了NUnit了

3、在工程中創(chuàng)建一個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步.為工程加一個類.

為工程加一個NumbersFixture類。這里是這個例子的代碼。

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的一個重要的屬性,意思是這個類包含了測試代碼。

[Test]是需要測試的方法

第3步:用NUnit運行

直接打開NUnit運行該dll文件或者建立你的Visual Studio工程,運用NUnit測試

一、直接打開NUnit運行該dll文件
打開NUnit,點擊file->open,選擇剛剛編譯的dll文件,這樣把文件加載到NUnit了,在它的界面的左邊,我們可以看到剛才編寫的測試函數(shù)AddOneNumber,選擇工程項目,或是文件,或是測試函數(shù),然后點擊界面右邊的Run,如果一路綠燈,OK,測試通過。

配置WAtin和NUnit一起使用,框架配置如下:
按上面步驟配置NUnit后,配置Watin
1:在工程中,References處右鍵選擇Add Reference,Browse選擇WatiN安裝目錄" WatiN-2.0.20.1089-net-2.0in"下的WatiN.Core.dll
2:添加一個引用:usingWatiN.Core;   如圖

此時完成配置了?梢杂肗Unit當(dāng)控制器運行watin腳本了。

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