【发布时间】:2023-04-07 11:53:02
【问题描述】:
我正在尝试使用 testfile (AccountTest.cs) 测试文件 (Account.cs)。我使用 Mono 框架(和 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:
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);
}
}
}
我通过以下方式编译这两个文件:
mcs -t:library Account.cs
mcs -t:library -r:nunit.framework,Account.dll AccountTest.cs
并分别获取Account.dll和AccountTest.dll。
要运行我使用的测试:
nunit-console AccountTest.dll
它运行正常,给我适当的失败和通过。
但是,现在我想使用 mono 的代码覆盖能力来评估这些测试。我正在阅读教程http://mono-project.com/Code_Coverage 来运行覆盖工具。要使用它,我需要编译成 *.exe 文件而不是 *.dll 文件。
如果有人可以帮助我处理 AccountTest.cs 文件的主类,我将能够在 exe 中编译它并从那里使用覆盖工具。
提前致谢。
【问题讨论】:
标签: c# macos mono nunit code-coverage