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

时间:2009-10-29 22:19:51

标签: 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;}
        }
    }
}

这是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中编译它,然后使用覆盖工具。

提前感谢你。

2 个答案:

答案 0 :(得分:6)

您指向的是正确的页面:

“要在使用nunit-console2直接运行单元测试时使用类似选项,请按如下方式指定MONO_OPTIONS:MONO_OPTIONS =” - profile = monocov:+ [MyAssembly]“nunit-console2 MyTestAssembly.dll”

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

答案 1 :(得分:1)

您可能希望试用Baboon我的新单声道代码覆盖率工具。 monocov和cov剖析器仅检查方法进入/退出,而Baboon能够检查程序中每个方法的每一行的覆盖范围,包括静态初始化器和私有成员。

$ echo assembly:MyTestFixture > ~/test.cfg

上面创建了一个配置文件,告诉baboon查看程序集中的代码。然后设置和环境变量并运行它: -

$ BABOON_CFG=$HOME/test.cfg covem.exe /opt/nunit/nunit-console.exe MyTestFixture.dll

给它一个旋转!最适合单声道3.x,您需要安装gtk-sharp才能运行GUI,或者您可以生成基本的HTML报告。

我一直在Linux上编写它,但它应该在OSX上运行良好。

功能请求/错误报告非常受欢迎!

相关问题