簡體   English   中英

使用mono和nunit測試的代碼覆蓋率

[英]Code coverage using mono and nunit tests

我正在嘗試使用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中編譯它,然后使用coverage工具。

提前感謝一噸。

您指向正確的頁面:

“要在使用nunit-console2直接運行單元測試時使用類似選項,請按如下方式指定MONO_OPTIONS:MONO_OPTIONS =” - profile = monocov:+ [MyAssembly]“nunit-console2 MyTestAssembly.dll”

您可以通過設置選項來運行單元測試並獲得代碼覆蓋率。

您可能想嘗試使用我的新單聲道代碼覆蓋工具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上運行良好。

功能請求/錯誤報告最受歡迎!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM