In the previous post, I wrote about installing MonoDevelop on Ubuntu 10.04 LTS. This post will be about installing NUnit integration for MonoDevelop.

I want to do some testing of my project with unit testing. I’ve search the web and many site (and videos) claim that MonoDevelop has build in unit testing support as well as class templates for NUnit, but I just couldn’t find anything in the IDE pertaining unit testing. After more research, I found that I need to install NUnit Plugin for Mono Develop (monodevelop-nunit). The following are the steps.

Install NUnit Plugin for Mono Develop.

Install the plugin from the software center.

Install NUnit Plugin for MonoDevelop Install NUnit Plugin for MonoDevelop

After installing, MonoDevelop will have an option for Unit Testing under the View menu as well as panels for running and showing results for tests.

MonoDevelop Unit Testing Menu MonoDevelop Unit Testing Menu

MonoDevelop Unit Test Panels MonoDevelop Unit Test and Result Panels

Create Test Project

To write unit tests, we need to first create a .NET library project for our test. Though NUnit plugin adds a new solution type to MonoDevelop called “NUnit Assembly Test Collection”, I have not yet figured out the intent or how to use this project type; therefore, we’ll house our tests in a regular library project.

After the test project has been created, the project needs to reference the nunit.framework assembly. Double click on references, go into All tab, and check the nunit.framework. I found that there are two copies of the assembly of same version. I believe one came with Mono and one just install the second time. I haven’t found difference between the two copies.

Writing Unit Tests

After having created a test project, we’re ready to write our unit tests. Create a new C# class in our test Project. Our test class will need to use the features in the NUnit.Framework namespace, which is defined in the nunit.framework assembly we referenced in the previous section.

In addition, the class need to be embellished with TestFixtureAttribute ([TestFixture]), and test method with TestAttribute ([Test]). The following is one of my test class.

using System;
using System.Data;
using NUnit.Framework;
using GeoStoreServer;

namespace GeoStoreServerTest
{
    [TestFixture]
    public class WindowSpatialFilterTest
    {

        [Test]
        public void TestPassesFilter()
        {
            SelectionTerm selTerm = new SelectionTerm("?loc");
            WindowSpatialFilter filter = new WindowSpatialFilter(selTerm, 10, 10, 30, 30);
            Assert.IsTrue(filter.PassesFilter(20, 20));
            Assert.IsTrue(filter.PassesFilter(10, 10));
            Assert.IsTrue(filter.PassesFilter(30, 30));
            Assert.IsTrue(filter.PassesFilter(10, 30));

            Assert.IsFalse(filter.PassesFilter(40, 40));
        }

        [Test]
        public void TestFilter()
        {
            SelectionTerm selTerm = new SelectionTerm("?loc");
            WindowSpatialFilter filter = new WindowSpatialFilter(selTerm,
            10, 10, 30, 30);
            DataTable table = new DataTable();
            table.Columns.Add("?loc", typeof(string));
            //DataRow row = table.NewRow();
            table.Rows.Add("\"15 15\"");
            table.Rows.Add ("\"15 40\""); //This should be filtered out
            table.Rows.Add ("\"20 20\"");
            table.Rows.Add ("\"40 40\""); //This should be filtered out
            table.AcceptChanges();
            table = filter.Filter(table);
            Assert.AreEqual(2, table.Rows.Count);
            Assert.AreEqual ("\"15 15\"", table.Rows[0][0].ToString());
        }

    }
}

References

  1. What the best setup latest Mono + MonoDevelop on Ubuntu 9.04?
  2. Setup a Test Project with NUnit and MonoDevelop