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
说 明:该实例是最早期的nunit版本中找到,在测试驱动的开发中它并不是一个很好的例子,但它能阐明使用nunit的最基本方法。
现在开始我们的例子。假设我们开始写一个银行业的应用程序,我们有一个基类—Account,Account主要负责资金的增加,撤销和转帐,下面是该类的代码
2{
3 public class Account
4 {
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 Balance
20 {
21 get{ return balance;}
22 }
23 }
24}
2{
3 using NUnit.Framework;
4
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}
这个类仅有的一个方法—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方法如下:
2{
3 destination.Deposit(amount);
4 Withdraw(amount);
5}
我们增加一些错误检测在我们的Account代码中,为balance设置一个最小值.依次来保护资金透支量
2public float MinimumBalance
3{
4 get{ return minimumBalance;}
5}
2{
3}
2[ExpectedException(typeof(InsufficientFundsException))]
3public 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的代码,让它抛出异常,按下面的实例修改TransferFunds方法.
2{
3 destination.Deposit(amount);
4 if(balance-amount < minimumBalance)
5 throw new InsufficientFundsException();
6 Withdraw(amount);
7}
2public 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}
2[Ignore("Decide how to implement transaction management")]
3public void TransferWithInsufficientFundsAtomicity()
4{
5 // code is the same
6}
上面是一些常用简单方法,依次来讲解nunit的使用过程,在以后的单元,我们会深入讲解nunit的使用!
相关文章
asp.net(C#)生成Code39条形码实例 条码枪可以扫描出
这篇文章主要介绍了asp.net(C#)生成Code39条形码实例 条码枪可以扫描出。需要的朋友可以过来参考下,希望对大家有所帮助2014-02-02使用本机IIS Express开发Asp.Net Core应用图文教程
IIS Express是一个Mini版的IIS,能够支持所有的Web开发任务,本篇经验将和大家介绍使用自定义主机名来访问运行在IIS Express上的站点程序的方法,希望对大家的工作和学习有所帮助2023-06-06asp.net使用Repeater控件中的全选进行批量操作实例
这篇文章主要介绍了asp.net使用Repeater控件中的全选进行批量操作方法,实例分析了Repeater控件的使用技巧,需要的朋友可以参考下2015-01-01利用ASP.NET MVC和Bootstrap快速搭建响应式个人博客站(一)
这篇文章主要介绍了利用ASP.NET MVC和Bootstrap快速搭建响应式个人博客站(一)的相关资料,需要的朋友可以参考下2016-06-06.NET调用控制台下生成的exe文件,传参及获取返回参数的思路及代码
.NET调用控制台下生成的exe文件,传参及获取返回参数的思路及代码,需要的朋友可以参考一下2013-06-06
最新评论