2009-10-29 106 views
11

我想用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:

mcs -t:library Account.cs 
mcs -t:library -r:nunit.framework,Account.dll 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); 
       } 
     } 

} 

我通過編譯這兩個文件.dll和AccountTest.dll分別。

要運行我使用測試:

nunit-console AccountTest.dll 

,它運行,因爲它應該給我相應的故障和傳遞。

不過,現在我想用單聲道的以評估這些測試的代碼覆蓋能力。我正在閱讀教程http://mono-project.com/Code_Coverage以運行覆蓋率工具。要使用它,我需要編譯成* .exe文件而不是* .dll文件。

如果有人可以幫助我的主類的AccountTest.cs文件,我將能夠再編譯EXE中,並從那裏使用覆蓋工具。

謝謝你提前一噸。

回答

6

您都指向正確的網頁:

「要使用類似的選項,而直接與NUnit的-console2運行單元測試,指定MONO_OPTIONS如下:MONO_OPTIONS =」 - 配置= monocov:+ [MyAssembly程序]」 NUnit的-console2 MyTestAssembly.dll」

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

1

你可能想嘗試Baboon我的新單代碼覆蓋工具。 monoov和cov分析器只檢查方法的入口/出口,而狒狒能夠檢查程序中每個方法的每一行的覆蓋率,包括靜態初始化器和私有成員。

$ echo assembly:MyTestFixture > ~/test.cfg 

上面創建了一個配置文件,它告訴狒狒在你的程序集中查看代碼。然後設置和環境變量並運行它: -

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

給它一個旋轉!在單聲道3.x上效果最佳,您需要安裝gtk-sharp才能運行GUI,或者您可以生成基本的html報告。

我已經寫在Linux上,但它應該在OSX上運行同樣出色。

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

相關問題