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.
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
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.
Tags: monodevelop, nunit, unit testing











Except where otherwise noted, content on this site is
January 10th, 2009 at 4:14 am
Clean and straight to the point. Cheers fella.
January 24th, 2009 at 3:49 pm
Thanks so much for posting this. Though if I found this maybe 2 hours earlier I would’ve saved myself … 2 hours.
April 16th, 2009 at 3:52 am
short and sweet, thanks man!
August 11th, 2009 at 11:12 am
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