Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 310
errorprone Avatar asked Oct 29 '09 22:10

errorprone


People also ask

Is Nunit a code coverage tool?

A) Jenkins B) None of the options C) Nunit D) Cobertura. Cobertura is a code coverage tool. This is actually a code coverage utility tool developed specifically for Java.

How do you find the code coverage of a test?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.

Is code coverage and unit testing same?

Unit tests help to ensure functionality and provide a means of verification for refactoring efforts. Code coverage is a measurement of the amount of code that is run by unit tests - either lines, branches, or methods.


2 Answers

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.

like image 100
Laurent Etiemble Avatar answered Sep 21 '22 15:09

Laurent Etiemble


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!

like image 20
IanNorton Avatar answered Sep 17 '22 15:09

IanNorton