开发者

Code coverage using mono and nunit tests

I'm trying to test a file (Account.cs) using testfile (AccountTest.cs). I run OSX 10.6 with Mono Framework (and nunit-console).

Below is 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;}
        }
    }
}

And here is 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);
                }
        }

}

I compile these two files by:

mcs -t:library Account.cs
mcs -t:library -r:nunit.framework,Account.dll AccountTest.cs

And get Account.dll and AccountTest.dll respectively.

To run the test I use:

nunit-console AccountTest.dll 

and it runs as it should, giving me the appropriate failures and passes.

However, now I want to use mono's code coverage ability to asses these tests. I'm reading the tutorial http://mono-project.com/Code_Coverage to run the coverage tools. And to use it I would need to compile into *.exe file rather than *.dll file.

If someone could help me with the main class of the AccountTest.cs file, I would be able to then compile it in exe and from there use the coverage tool.

Thanks a tonne in advance.


You are pointing to the right page:

"To use similar options while running unit tests directly with nunit-console2, specify MONO_OPTIONS as follows: MONO_OPTIONS="--profile=monocov:+[MyAssembly]" nunit-console2 MyTestAssembly.dll"

You can run your unit tests and get code coverage by setting the option.


You might like to try out Baboon my new mono code coverage tool. The monocov and cov profilers only check for method entry/exit while Baboon is able to check the coverage of each line of each method in your program, including static initializers and private members.

$ echo assembly:MyTestFixture > ~/test.cfg

The above creates a config file that tells baboon to look at code in your assembly. Then set and environment variable and run it:-

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

Give it a spin! Works best on mono 3.x, You'll need gtk-sharp installed to run the GUI or you can generate a basic html report.

I've been writing it on Linux but it should run equally well on OSX.

Feature requests/bug reports most welcome!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜