您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > Nunit
NUnit使用之HelloWorld
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/3/29 14:52:23 ] 推薦標(biāo)簽:

  首先要有.net2.0或3.5都可以.然后下載NUnit(msi),版本無所謂.NUnit官方網(wǎng)站上有新版本2.5.1.

  安裝完后,在vs2005或2008中新建一個(gè)項(xiàng)目HelloNUnitTest(項(xiàng)目類型無所謂,僅作測試用).

    然后編寫一個(gè)函數(shù)Add,實(shí)現(xiàn)普通的加法運(yùn)算.

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4
  5 namespace HelloNUnitTest
  6 {
  7     public class Class1
  8     {
  9         public double Add(double a, double b)
  10         {
  11             return a + b;
  12         }
  13     }
  14 }

  接下來添加NUnit的引用,一般只需添加nunit.framework可以了.

  接著添加一個(gè)空類Class1Test(測試一般命名為被測試的類名 + Test)

  接著編寫下面的代碼:
  1 using System;
  2 using NUnit.Framework;
  3
  4 namespace HelloNUnitTest
  5 {
  6     [TestFixture]
  7     public class Class1Test
  8     {
  9         [Test]
  10         public void TestAdd()
  11         {
  12             double result;
  13             double expected = 4;
  14             Class1 testclass = new Class1();
  15             result = testclass.Add(1.5,2.5);
  16
  17             Assert.AreEqual(expected,result);
  18         }
  19     }
  20 }

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