使用NUnit进行单元测试
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");
}
}
引自:http://blog.csdn.net/davidullua/archive/2005/05/11/373694.aspx