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");
}
/// <summary>
/// SetTest
/// </summary>
[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");
}
}