使用NUnit进行单元测试

2016-01-29 12:58 16 1 收藏

使用NUnit进行单元测试,使用NUnit进行单元测试

【 tulaoshi.com - ASP.NET 】


NUnit适用于.Net开发中的单元测试,使用步骤如下
1、下载NUnit-2.2.2并解压或安装。
2、VS.Net中建立 项目,添加对NUnit.Framework.dll的引用,
3、创建新类AccountTest 测试已存在的类Account
4、将代码编译为DLL或exe文件,在nunit-gui主程序中打开编译过的程序,选中要运行的Test Case点Run
另外,配合DCGWin,可将NUnit的测试结果(保存的XML文件)生成html格式的测试报告.
public class Account { private float balance;
public void Deposit(float amount) { balance += amount; }
public void Withdraw(float amount) { balance -= amount; }
public void TransferFunds(Account destination, float amount) { this.balance-=amount; destination.balance+=amount; }
public float Balance { get { return balance; } set { balance=value; } } }
[TestFixture(Description="帐号测试")] public class AccountTest {
[Test(Description="转帐测试")] public void TransferFunds() { Account source = new Account(); source.Deposit(200.00F); Account destination = new Account(); destination.Deposit(150.00F);
source.TransferFunds(destination, 100.00F); Assert.AreEqual(250.00F, destination.Balance);//should be 250 Assert.AreEqual(100.00F, source.Balance); Console.WriteLine("Funds Tranfer Event Tested Successfully"); }

[Test(Description="Account.Balance的Get属性")] public void GetTest() { Account source=new Account(); source.Deposit(100.00F); Assert.AreEqual(100,source.Balance); Console.WriteLine("Balance Attributes Get Tested Successfully"); }
/// /// SetTest /// [Test(Description="Account.Balance的set属性测试")] public void SetTest() { Account source=new Account(); source.Balance=100; Assert.AreEqual(100,source.Balance); source.Balance-=50; Console.WriteLine("Balance Attributes Set Tested Successfully"); Assert.AreEqual(50,source.Balance);
}
[Test(Description="取款测试")] public void WithDrawTest() { Account source=new Account(); source.Deposit(100.00F); source.Withdraw(20.00F); Assert.AreEqual(80,source.Balance); Console.WriteLine("Balance Attributes WithDraw Tested Successfully"); }
[Test(Description="存款测试")] public void DepositTest() { Account source=new Account(); source.Deposit(100.00F); Assert.AreEqual(100,source.Balance); Console.WriteLine("Balance Attributes Deposit Tested Successfully"); } }

来源:https://www.tulaoshi.com/n/20160129/1488323.html

延伸阅读
        摘要         不能因为Java Micro Edition缺乏反射能力就说Java Micro Edition开发者无法利用JUnit风格测试的优点。其实,借助于具有JUnit风格的其它一些框架和工具,Java ME开发人员仍然能够改进Java ME应用程序的开发质量。本系列文章(两篇)正是想...
极限编程(XP)越来越进入程序员的眼球,TDD(Test Drived Design)也越来越普及,UT(Unit Testing)是TDD的第一步,主要面向的是一线的开发人员,而不是项目经理、系统设计与分析人员甚至是测试人员,当然UT的一些方法也可以用于后续的测试,但从概念上来讲那已经不算UT了。UT是开发者写的一小段代码,用于检验被测代码的一个很小的、很明确的功能...
标签: Web开发
什么是单元测试? 单元测试又称为模块测试,是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作。单元测试主要是用来检验程式的内部逻辑,也称为个体测试、结构测试或逻辑驱动测试。通常由撰写程式码的程式设计师负责进行。 通常来说,程式設計師每修改一次程式就會進行最少一次單元測試,在編寫程式的過程中前後很可能要進行...
   最近在做一个项目,我们使用的是一些看似很标准的WEB结构,DAO(数据访问)+HELPER(处理业务操作)+ACTION(调用HELPER层)+struts的控制系统,页面使用struts标签和部分自定义标签完成显示.  进入测试阶段后 1,DAO测试--junit的确非常不错,我们使用eclipse+junit.jar,很满足的完成了这部分检测 2.helper...
标签: Web开发
NUnit 是为 .NET 框架生成的开放源代码单元测试框架。NUnit 使您可以用您喜欢的语言编写测试,从而测试应用程序的特定功能。当您首次编写代码时,单元测试是一种测试代码功能的很好方法,它还提供了一种对应用程序进行回归测试的方法。NUnit 应用程序提供了一个用于编写单元测试的框架,以及一个运行这些测试和查看结果的图形界面。   ...

经验教程

60

收藏

76
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部