使用mono和nunit测试的代码覆盖率

err*_*one 11 c# macos mono nunit code-coverage

我正在尝试使用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;}
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是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);
                }
        }

}
Run Code Online (Sandbox Code Playgroud)

我编译这两个文件:

mcs -t:library Account.cs
mcs -t:library -r:nunit.framework,Account.dll AccountTest.cs
Run Code Online (Sandbox Code Playgroud)

并分别获取Account.dll和AccountTest.dll.

要运行测试,我使用:

nunit-console AccountTest.dll 
Run Code Online (Sandbox Code Playgroud)

并且它应该运行,给我适当的失败和通过.

但是,现在我想使用mono的代码覆盖能力来评估这些测试.我正在阅读教程http://mono-project.com/Code_Coverage来运行覆盖工具.要使用它,我需要编译成*.exe文件而不是*.dll文件.

如果有人可以帮我处理AccountTest.cs文件的主类,我可以在exe中编译它,然后使用coverage工具.

提前感谢一吨.

Lau*_*ble 6

您指向正确的页面:

"要使用类似的选项,而直接使用Nunit-console2运行单元测试中,指定MONO_OPTIONS如下:MONO_OPTIONS =" - 轮廓= monocov:+ [MyAssembly程序] "的NUnit-console2 MyTestAssembly.dll"

您可以通过设置选项来运行单元测试并获得代码覆盖率.