NUnit 是为 .NET 框架生成的开放源代码单元测试框架。NUnit 使您可以用您喜欢的语言编写测试,从而测试应用程序的特定功能。当您首次编写代码时,单元测试是一种测试代码功能的很好方法,它还提供了一种对应用程序进行回归测试的方法。NUnit 应用程序提供了一个用于编写单元测试的框架,以及一个运行这些测试和查看结果的图形界面。
下载地址:http://sourceforge.net/projects/nunitaddin
本事例所用版本:http://www.cnblogs.com/Files/young18/nunit2.3.rar
NUnit Quick Start
原文档:http://www.nunit.org
翻 译:Young.J
说 明:该实例是最早期的nunit版本中找到,在测试驱动的开发中它并不是一个很好的例子,但它能阐明使用nunit的最基本方法。
现在开始我们的例子。假设我们开始写一个银行业的应用程序,我们有一个基类—Account,Account主要负责资金的增加,撤销和转帐,下面是该类的代码
1namespacebank
2{
3publicclassAccount
4{
5privatefloatbalance;
6publicvoidDeposit(floatamount)
7{
8balance+=amount;
9}
10
11publicvoidWithdraw(floatamount)
12{
13balance-=amount;
14}
15
16publicvoidTransferFunds(Accountdestination,floatamount)
17{}
18
19publicfloatBalance
20{
21get{returnbalance;}
22}
23}
24} 在我们来写一个需要测试的类—AccountTest,我们第一个测试的方法是TransferFunds 1namespacebank
2{
3usingNUnit.Framework;
4
5[TestFixture]
6publicclassAccountTest
7{
8[Test]
9publicvoidTransferFunds()
10{
11Accountsource=newAccount();
12source.Deposit(200.00F);
13Accountdestination=newAccount();
14destination.Deposit(150.00F);
15source.TransferFunds(destination,100.00F);
16Assert.AreEqual(250.00F,destination.Balance);
17Assert.AreEqual(100.00F,source.Balance);
18}
19}
20} 现在做的第一件事是声明这个类有一个[TestFixture]属性,通过这种方法显示这个类包含测试代码(此属性可以被继承),这个类必须是public类且其派生内没有限制,这个类当然必须有一个默认构造函数。
这个类仅有的一个方法—TransferFunds,有一个[Test]属性,显示他是一个测试方法,该方法返回void,没有参数,在这个方法里我们对测试对象作了必须的初始化,Assert类定义了一些方法的集合体,用它来检测设置条件,在我们的例子中,我们用AreEqual方法确保后面两个账户的转帐都有一个正确的剩余资金(这些是一些重载方法,这个例子中用的版本包含下面几个参数,第一个参数是期望值,第二个参试是真实值),
编译运行这个例子,假设你编译你的代码为bank.dll,运行NUnit Gui,选择File->Open menu item,载入刚才编译过的dll文件,点击run,我们可以看到测试条变成红色—我们的测试失败了,在“Errors and Failures”面板显示一面信息:
TransferFunds : expected <250> but was <150>
这个现象是我们所期望的,测试失败的原因是我们没有实现TransferFunds方法,现在我们开始让它工作,修改你的TransferFunds方法如下: 1publicvoidTransferFunds(Accountdestination,floatamount)
2{
3destination.Deposit(amount);
4Withdraw(amount);
5}现在我们再次编译代码,并在GUI中运行,我们何以看到测试条变绿!测试成功!
我们增加一些错误检测在我们的Account代码中,为balance设置一个最小值.依次来保护资金透支量 1privatefloatminimumBalance=10.00F;
2publicfloatMinimumBalance
3{
4get{returnminimumBalance;}
5} 增加一个表明透支的异常; 1publicclassInsufficientFundsException:ApplicationException
2{
3}增加一个测试方法到AccountTest类中 1[Test]
2[ExpectedException(typeof(InsufficientFundsException))]
3publicvoidTransferWithInsufficientFunds()
4{
5Accountsource=newAccount();
6source.Deposit(200.00F);
7Accountdestination=newAccount();
8destination.Deposit(150.00F);
9source.TransferFunds(destination,300.00F);
10}这个测试方法的[Test]属性有一个 [ExpectedException]属性,这表明这段测试代码期望得到某一类型的异常,如果这种异常没有出现在执行过程中,这车是失败,现在编译代码,启动NUnit Gui,这是测试条变红,提示错误信息:
TransferWithInsufficentFunds : InsufficientFundsException was expected
让我们重新配置Account的代码,让它抛出异常,按下面的实例修改TransferFunds方法. 1publicvoidTransferFunds(Accountdestination,floatamount)
2{
3destination.Deposit(amount);
4if(balance-amount<minimumBalance)
5thrownewInsufficientFundsException();
6Withdraw(amount);
7}编译,运行测试-测试条变绿,成功了,但是,我们看看这个代码,我们仅仅写了我们可以看到的转帐操作中的错误,现在让我们来写一个测试来证实我们不确定的错误,添加下面一个测试方法 1[Test]
2publicvoidTransferWithInsufficientFundsAtomicity()
3{
4Accountsource=newAccount();
5source.Deposit(200.00F);
6Accountdestination=newAccount();
7destination.Deposit(150.00F);
8try
9{
10source.TransferFunds(destination,300.00F);
11}
12catch(InsufficientFundsExceptionexpected)
13{
14}
15Assert.AreEqual(200.00F,source.Balance);
16Assert.AreEqual(150.00F,destination.Balance);
17}编译运行—红色测试条,我们算错了300元,代码显示正确的结果是150元,但账户显示确是450,那么怎样修补错误,能不能加一段最小基金检测在资金处理之前呢?我们可以在catch块中加以一些修补方法,或依靠我们的管理人员修复对象的规定,我们需要在多方面回答这些问题,但不是在现在,那在此期间我们可以怎么做呢?移出它?一个最好的方法是忽视它,添加以下属性在你的测试方法中 1[Test]
2[Ignore("Decidehowtoimplementtransactionmanagement")]
3publicvoidTransferWithInsufficientFundsAtomicity()
4{
5//codeisthesame
6}编译测试代码—黄色,点击"Tests Not Run",我们可以看到bank.AccountTest.TransferWithInsufficientFundsAtomicity() 在被忽视的测试列表里。
上面是一些常用简单方法,依次来讲解nunit的使用过程,在以后的单元,我们会深入讲解nunit的使用!