Home About

December 30th, 2008

Using NUnit with MonoDevelop - 4

Unit testing with NUnit is deeply integrated into Mono. The Mono code itself uses NUnit extensively to test its own functionality. In this post we look at how you can enable unit testing for your own code under MonoDevelop. In an earlier post I looked at how to install NUnit for Visual Studio 2008 Express, in this post we do the same for MonoDevelop.

Creating your first NUnit test

We need to create a new solution to test if this works and we need to add a reference to NUnit to be able to use the code. This is the only “difficult” bit, the MonoDevelop documentation is sparse at best on how to include external assemblies. A dedicated menu option would have been nice.

To be able to add an assembly or package to an existing project you need to right click the References item on the Solution panel and then click “Edit References”. Click the “NUnit.Core” and “NUnit.Framework” selections to include them into your solution.

Including NUnit as an External Assembly to MonoDevelop

Testing the NUnit Demo code

Below I have included a little bit of demo code that tests if this works properly. The code is mostly borrowed from the NUnit website demo, I have just for the sake of it included a class containing “Main” so that the code actually compiles and runs.

using System;
using NUnit.Framework;

namespace bank
{
    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)
        {
        }

        public float Balance
        {
            get { return balance; }
        }
    }
}

namespace bank
{
    [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]
        public void DepositFunds()
        {
            Account source = new Account();
            source.Deposit(200.00F);
            Assert.AreEqual(200.00F, source.Balance);
        }

    }
}

namespace UnitTestDemo
{
    public class MyAccountingSoftware
    {

        public static void Main()
        {
            bank.Account DemoAccount = new bank.Account();
            DemoAccount.Deposit(1000.00F);
            DemoAccount.Withdraw(500.50F);
            Console.WriteLine("Our account balance is {0}", DemoAccount.Balance);
        }

    }
}

In the above code the actual unit testing is done by the “AccountTest” class, for simplicity we keep this in the same file but usually you would keep a separate directory with all of your unit tests, separate from the actual code.Note that because the “TransferFunds” function in the code above is poorly implemented (eg. its empty) the Unit test will hopefully (!!) fail. The second unit test in DepositFunds should succeed.

How to run NUnit tests from within MonoDevelop

Running NUnit from within MonoDevelop

MonoDevelop allows you to run the NUnit tests directly in the GUI environment. Select “View > Unit Tests” to see an overview of all the defined tests in the left panel. Right click on a test , or a group of tests and select “Run Test”. Click on “View > NUnit Test Results” to see which tests passed and which failed.

Be Sociable, Share!

Tags: , ,

4 Responses to “Using NUnit with MonoDevelop”

  1. Raluxgaza Says:

    Clean and straight to the point. Cheers fella.

  2. jbu Says:

    Thanks so much for posting this. Though if I found this maybe 2 hours earlier I would’ve saved myself … 2 hours.

  3. danieru Says:

    short and sweet, thanks man!

  4. leblanc meneses Says:

    mono

    in regards to your sample I would have done the following:
    IAccountRepository
    void Deposit(Account a, Decimal total);
    void Withdraw(Account a, Decimal total);
    void Balance(Account a);
    void TransferFunds(Account source, Account destination, Decimal amount);

    concrete providers would be:
    PgAccountRepository
    MySqlAccountRepository

    ect.
    http://www.robusthaven.com


Most popular
Recent Comments
  • ARS: great plugin! I love it! but, it will be so nice if you can add attribute ‘title’ as one of...
  • Nelson: Saved me from doing it myself. Good article.
  • andy: i am currently playing taiwanese server wow in 奈辛瓦里(PVP) and i would like to realm transfer to somewhere there...
  • berties: any english speaking playing on a taiwanese server?
  • web application development: has C# search volume really so constant over the years? really surprising.