Day 2 – Dot NET on Linux – Unit testing with NUnit in MonoDevelop

Intro

Unit testing should play a major part in any software development and we are not going to cut corners here.

The Source Code

This post is part of my Dot Net On Linux posts. All the source the source code from the series of posts is available on GitHub here

Why Unit Test

Unit testing is an important part of any source branch. I personally prefer TDD; it makes you think about  the code you are writing. Understanding how to test the code you are writing allows creation of better structured, better working and provides less chance of introducing bugs during the codes lifetime. Fitting unit tests into code post development often requires refactoring to allow testing of expensive entities such as the database. Refactoring without unit tests requires a complete understanding of the code being edited; which is not always possible. In conjunction with mocking frameworks we can unit test our code without a database or other expensive resources.

Why NUnit

MonoDevelop comes with a NUnit add in module which allows integration of NUnit and MonoDevelop; we can run and validate unit tests in situ.

Installation

To install the NUnit plugin run the following in a terminal or install via your package manager such as Synaptic.

sudo apt-get install monodevelop-nunit monodoc-nunit-manual

 

Setting Up

Create a new solution and call it MathsLibrary.

Create a new test project by right clicking on the MathsLibrary solution and selecting Add –> Add New Project.

If the option is not available make sure you are on the solution and not the project or the Work Space item. One of the annoying things about MonoDevelop is that the many menu items are context driven.

From the new project dialog select NUnit Library Project.

Image

Add in a reference to the NUnit framework;  highlight the unit test project, right click and select Edit Reference. In the edit references dialog add in nunit.framework assembly from the packages tab. If you have multiple versions add the highest version.

Image

The Code

Test classes requires the TextFixture attribute, test functions require the the Test attributes.

using System;
using System.Collections.Generic;
using MathsLibrary.Average;
using NUnit.Framework;

namespace MathsLibrary.Tests
{
	[TestFixture()]
	public class MeanTests
	{
		[Test()]
		public void CanCalculateMean()
		{

			var data = new List{ 1.1m, 2.2m, 3.3m, 4.4m, 5.5m };

			var modeValue = Mean.Calculate(data);

			Assert.AreEqual(modeValue, 3.3);
		}

		[Test()]
		public void WillThrowArgumentNullExceptionWhenNull()
		{
			Assert.Throws(() => Mean.Calculate(null));
		}

		[Test()]
		public void WillThrowArgumentExceptionWhenEmptyCollection()
		{
			Assert.Throws(() => Mean.Calculate(new List()));
		}
	}
}

You would need to write the code stubs on the other side and stub them by throwing a NotImplementedException.

Image

Running The Tests

We are ready to run our tests. View Menu –> Pads –> Unit Tests

To run all tests –> Hit the up arrow

To run all tests contained by the selected parent tree element –> Hit the cog wheel or right click and –> Run Test.

To run the test in debug right click a test in the  tree view and select –> Run Tests With –> Mono Soft Debugger. If you have created any break points they will be hit.

To add a break point you need to click the line in the far left of the margin.

Two other handy elements are Right Click –>Go To Failure / Show Test Source Code.

Image

You then need to write the code you are testing and then run the tests again.

Image

Note: To use Linq you need to add a reference to System.Core.

The only other worth note is the results pad which provides some basic analysis of the test run.

Image

Advertisements

One thought on “Day 2 – Dot NET on Linux – Unit testing with NUnit in MonoDevelop

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s