【问题标题】:Code coverage using mono and nunit tests使用 mono 和 nunit 测试的代码覆盖率
【发布时间】: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


    【解决方案1】:

    您正在指向正确的页面:

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

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

    【讨论】:

      【解决方案2】:

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

      $ echo assembly:MyTestFixture > ~/test.cfg
      

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

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

      试一试!在 mono 3.x 上效果最好,您需要安装 gtk-sharp 来运行 GUI,或者您可以生成基本的 html 报告。

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

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

      【讨论】:

        猜你喜欢
        • 2011-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-18
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 2012-02-23
        相关资源
        最近更新 更多