我想用testfile(AccountTest.cs)測試一個文件(Account.cs)。我使用Mono Framework(和nunit-console)運行OSX 10.6。使用單聲道和單元測試的代碼覆蓋
下面是Account.cs
namespace bank
{
using System;
public class InsufficientFundsException : ApplicationException
{
}
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)
{
destination.Deposit(amount);
Withdraw(amount);
}
public float Balance
{
get { return balance;}
}
private float minimumBalance = 10.00F;
public float MinimumBalance
{
get{ return minimumBalance;}
}
}
}
這裏是AccountTest.cs:
mcs -t:library Account.cs
mcs -t:library -r:nunit.framework,Account.dll AccountTest.cs
並獲得帳號:
namespace bank
{
using NUnit.Framework;
[TestFixture]
public class AccountTest
{
[Test]
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);
Assert.AreEqual(100.00F, source.Balance);
}
[Test]
[ExpectedException(typeof(InsufficientFundsException))]
public void TransferWithInsufficientFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Account destination = new Account();
destination.Deposit(150.00F);
source.TransferFunds(destination, 300.00F);
}
}
}
我通過編譯這兩個文件.dll和AccountTest.dll分別。
要運行我使用測試:
nunit-console AccountTest.dll
,它運行,因爲它應該給我相應的故障和傳遞。
不過,現在我想用單聲道的以評估這些測試的代碼覆蓋能力。我正在閱讀教程http://mono-project.com/Code_Coverage以運行覆蓋率工具。要使用它,我需要編譯成* .exe文件而不是* .dll文件。
如果有人可以幫助我的主類的AccountTest.cs文件,我將能夠再編譯EXE中,並從那裏使用覆蓋工具。
謝謝你提前一噸。