当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 使用NUnit进行单元测试

ASP.NET
many-to-many多对多映射
.NETRemotingChannelListener
转载李建忠老师的一篇文章
原创ColorComboBox控件
用IDisposable接口释放.NET资源
c#v2.0 扩展特性 翻译1
XPath中如何比较不同类型的对象
用哈希表搜索对象
C#陷阱int i = 10; i += i++; i =
Make a window that pops up from taskbar
获取网站返回的头部信息
自己动手写屏保
在DataGrid中添加Radio单选按钮列
实现性能目标的几种方法
增强.NETFramework中线程的功能
ASP.NET 2.0,写无限级下拉菜单不再难
book_dotnet
用.NET武装你的头脑
准备你的发布阶段
移植到.NET

ASP.NET 中的 使用NUnit进行单元测试


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 60 ::
收藏到网摘: n/a


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"); } }