nunit使用指南之—NUnit Quick Start
下載地址:http://sourceforge.net/projects/nunitaddin
本事例所用版本:http://www.cnblogs.com/Files/young18/nunit2.3.rar
NUnit Quick Start
原文檔:http://www.nunit.org
翻 譯:Young.J
說 明:該實(shí)例是最早期的nunit版本中找到,在測試驅(qū)動(dòng)的開發(fā)中它并不是一個(gè)很好的例子,但它能闡明使用nunit的最基本方法。
現(xiàn)在開始我們的例子。假設(shè)我們開始寫一個(gè)銀行業(yè)的應(yīng)用程序,我們有一個(gè)基類—Account,Account主要負(fù)責(zé)資金的增加,撤銷和轉(zhuǎn)帳,下面是該類的代碼
namespace bank2


{3
public class Account4

{5
private float balance;6
public void Deposit(float amount)7

{8
balance+=amount; 9
}10

11
public void Withdraw(float amount)12

{ 13
balance-=amount; 14
}15

16
public void TransferFunds(Account destination, float amount)17

{ }18

19
public float Balance20

{ 21

get
{ return balance;} 22
}23
}24
}
namespace bank2


{3
using NUnit.Framework;4

5
[TestFixture]6
public class AccountTest7

{8
[Test]9
public void TransferFunds()10

{11
Account source = new Account();12
source.Deposit(200.00F);13
Account destination = new Account();14
destination.Deposit(150.00F);15
source.TransferFunds(destination, 100.00F);16
Assert.AreEqual(250.00F, destination.Balance);17
Assert.AreEqual(100.00F, source.Balance);18
}19
}20
}這個(gè)類僅有的一個(gè)方法—TransferFunds,有一個(gè)[Test]屬性,顯示他是一個(gè)測試方法,該方法返回void,沒有參數(shù),在這個(gè)方法里我們對測試對象作了必須的初始化,Assert類定義了一些方法的集合體,用它來檢測設(shè)置條件,在我們的例子中,我們用AreEqual方法確保后面兩個(gè)賬戶的轉(zhuǎn)帳都有一個(gè)正確的剩余資金(這些是一些重載方法,這個(gè)例子中用的版本包含下面幾個(gè)參數(shù),第一個(gè)參數(shù)是期望值,第二個(gè)參試是真實(shí)值),
編譯運(yùn)行這個(gè)例子,假設(shè)你編譯你的代碼為bank.dll,運(yùn)行NUnit Gui,選擇File->Open menu item,載入剛才編譯過的dll文件,點(diǎn)擊run,我們可以看到測試條變成紅色—我們的測試失敗了,在“Errors and Failures”面板顯示一面信息:
TransferFunds : expected <250> but was <150>
這個(gè)現(xiàn)象是我們所期望的,測試失敗的原因是我們沒有實(shí)現(xiàn)TransferFunds方法,現(xiàn)在我們開始讓它工作,修改你的TransferFunds方法如下:
public void TransferFunds(Account destination, float amount)2


{3
destination.Deposit(amount);4
Withdraw(amount);5
}我們增加一些錯(cuò)誤檢測在我們的Account代碼中,為balance設(shè)置一個(gè)最小值.依次來保護(hù)資金透支量
private float minimumBalance = 10.00F;2
public float MinimumBalance3


{4

get
{ return minimumBalance;}5
}
public class InsufficientFundsException : ApplicationException2


{3
}
[Test]2
[ExpectedException(typeof(InsufficientFundsException))]3
public void TransferWithInsufficientFunds()4


{5
Account source = new Account();6
source.Deposit(200.00F);7
Account destination = new Account();8
destination.Deposit(150.00F);9
source.TransferFunds(destination, 300.00F);10
}TransferWithInsufficentFunds : InsufficientFundsException was expected
讓我們重新配置Account的代碼,讓它拋出異常,按下面的實(shí)例修改TransferFunds方法.
public void TransferFunds(Account destination, float amount)2


{3
destination.Deposit(amount);4
if(balance-amount < minimumBalance)5
throw new InsufficientFundsException();6
Withdraw(amount);7
}
[Test]2
public void TransferWithInsufficientFundsAtomicity()3


{4
Account source = new Account();5
source.Deposit(200.00F);6
Account destination = new Account();7
destination.Deposit(150.00F);8
try9

{10
source.TransferFunds(destination, 300.00F);11
}12
catch(InsufficientFundsException expected)13

{14
}15
Assert.AreEqual(200.00F,source.Balance);16
Assert.AreEqual(150.00F,destination.Balance);17
}
[Test]2
[Ignore("Decide how to implement transaction management")]3
public void TransferWithInsufficientFundsAtomicity()4


{5
// code is the same6
}上面是一些常用簡單方法,依次來講解nunit的使用過程,在以后的單元,我們會(huì)深入講解nunit的使用!
相關(guān)文章
設(shè)置ASP.NET頁面的運(yùn)行超時(shí)時(shí)間詳細(xì)到單個(gè)頁面及站點(diǎn)
這篇文章主要介紹了如何設(shè)置ASP.NET頁面的運(yùn)行超時(shí)時(shí)間,包括全局超時(shí)時(shí)間、單個(gè)站點(diǎn)超時(shí)時(shí)間、單個(gè)頁面請求超時(shí)時(shí)間,需要的朋友可以參考下2014-06-06
asp.net 長文章通過設(shè)定的行數(shù)分頁
長文章通過設(shè)定的行數(shù)來實(shí)現(xiàn)分頁的代碼。2009-12-12
asp.net的IndexOf,LastIndexOf,IndexOfAny和LastIndexOfAny的用法
IndexOf,LastIndexOf,IndexOfAny和LastIndexOfAny的功能,是定位字符或定位子串2012-10-10
Asp.net實(shí)現(xiàn)無刷新調(diào)用后臺實(shí)體類數(shù)據(jù)并以Json格式返回
本文主要分享了Asp.net實(shí)現(xiàn)無刷新調(diào)用后臺實(shí)體類數(shù)據(jù)并以Json格式返回的具體實(shí)例方法,具有一定的參考價(jià)值,有需要的朋友可以看下2016-12-12
VS2017 Cordova Ionic2 移動(dòng)開發(fā)環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了VS2017 Cordova Ionic2 移動(dòng)開發(fā)環(huán)境搭建教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
ASP.NET Core2讀寫InfluxDB時(shí)序數(shù)據(jù)庫的方法教程
Influxdb是一個(gè)開源的分布式時(shí)序、時(shí)間和指標(biāo)數(shù)據(jù)庫,使用go語言編寫,無需外部依賴,下面這篇文章主要給大家介紹了關(guān)于ASP.NET Core2讀寫InfluxDB時(shí)序數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下2018-11-11
在asp.net core中使用類似Application的服務(wù)的實(shí)現(xiàn)
這篇文章主要介紹了在asp.net core中使用類似Application的服務(wù)的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02

