nunit使用指南——NUnit Quick Start
NUnit 是为 .NET 框架生成的开放源代码单元测试框架。NUnit 使您可以用您喜欢的语言编写测试,从而测试应用程序的特定功能。当您首次编写代码时,单元测试是一种测试代码功能的很好方法,它还提供了一种对应用程序进 行回归测试的方法。NUnit 应用程序提供了一个用于编写单元测试的框架,以及一个运行这些测试和查看结果的图形界面。下载地址:http://sourceforge.net/projects/nunitaddin
本事例所用版本:https://files.cnblogs.com/young18/nunit2.3.rar
NUnit Quick Start
原文档:http://www.nunit.org
翻 译:Young.J
说 明:该实例是最早期的nunit版本中找到,在测试驱动的开发中它并不是一个很好的例子,但它能阐明使用nunit的最基本方法。
现在开始我们的例子。假设我们开始写一个银行业的应用程序,我们有一个基类—Account,Account主要负责资金的增加,撤销和转帐,下面是该类的代码
1
namespace bank
2
{
3
public class Account
4
{
5
private float balance;
6
public void Deposit(float amount)
7
{
8
balance+=amount;
9
}
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
public void Withdraw(float amount)
12
{
13
balance-=amount;
14
}
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
public void TransferFunds(Account destination, float amount)
17
{ }
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
public float Balance
20
{
21
get{ return balance;}
22
}
23
}
24
}
在我们来写一个需要测试的类—AccountTest,我们第一个测试的方法是TransferFunds
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
namespace bank
2
{
3
using NUnit.Framework;
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
[TestFixture]
6
public class AccountTest
7
{
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
}
现在做的第一件事是声明这个类有一个[TestFixture]属性,通过这种方法显示这个类包含测试代码(此属性可以被继承),这个类必须是public类且其派生内没有限制,这个类当然必须有一个默认构造函数。![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这个类仅有的一个方法—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方法如下:
1
public void TransferFunds(Account destination, float amount)
2
{
3
destination.Deposit(amount);
4
Withdraw(amount);
5
}
现在我们再次编译代码,并在GUI中运行,我们何以看到测试条变绿!测试成功!![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
我们增加一些错误检测在我们的Account代码中,为balance设置一个最小值.依次来保护资金透支量
1
private float minimumBalance = 10.00F;
2
public float MinimumBalance
3
{
4
get{ return minimumBalance;}
5
}
增加一个表明透支的异常;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
public class InsufficientFundsException : ApplicationException
2
{
3
}
增加一个测试方法到AccountTest类中
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
[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
}
这个测试方法的[Test]属性有一个 [ExpectedException]属性,这表明这段测试代码期望得到某一类型的异常,如果这种异常没有出现在执行过程中,这车是失败,现在编译代码,启动NUnit Gui,这是测试条变红,提示错误信息:![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
TransferWithInsufficentFunds : InsufficientFundsException was expected
让我们重新配置Account的代码,让它抛出异常,按下面的实例修改TransferFunds方法.
1
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
}
编译,运行测试-测试条变绿,成功了,但是,我们看看这个代码,我们仅仅写了我们可以看到的转帐操作中的错误,现在让我们来写一个测试来证实我们不确定的错误,添加下面一个测试方法
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
[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
try
9
{
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
}
编译运行—红色测试条,我们算错了300元,代码显示正确的结果是150元,但账户显示确是450,那么怎样修补错误,能不能加一段最小基金检测在资
金处理之前呢?我们可以在catch块中加以一些修补方法,或依靠我们的管理人员修复对象的规定,我们需要在多方面回答这些问题,但不是在现在,那在此期
间我们可以怎么做呢?移出它?一个最好的方法是忽视它,添加以下属性在你的测试方法中
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
[Test]
2
[Ignore("Decide how to implement transaction management")]
3
public void TransferWithInsufficientFundsAtomicity()
4
{
5
// code is the same
6
}
编译测试代码—黄色,点击"Tests Not Run",我们可以看到bank.AccountTest.TransferWithInsufficientFundsAtomicity() 在被忽视的测试列表里。![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
上面是一些常用简单方法,依次来讲解nunit的使用过程,在以后的单元,我们会深入讲解nunit的使用!