Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Setup a Test Project with NUnit and MonoDevelop

4.20/5 (3 votes)
24 Mar 2009CPOL3 min read 75K   373  
How to set up an NUnit test project within MonoDevelop

Introduction 

This article is an attempt at creating some documentation regarding NUnit and MonoDevelop. I decided to create it after an exercise in futility searching the Internet; maybe there is documentation out there but I couldn't find it. This article is based on NUnit 2.2.8.0 and MonoDevelop 1.0 running on UbuntuStudio 8.04. What was most confusing to me is that MonoDevelop has a project template called “NUnit assembly test collection”; if anyone can shed light on how or what that is used for, I'd love to update this article or link to another. In summary, it's not exactly obvious how to use NUnit with MonoDevelop and I'm not sure this is a best practice but it worked for me.

One thing I did come across as far as guidance is that it's recommended to have a test project for each project in a solution. Personally I like this concept because it clearly and visually separates the test from the real code. Whether or not you include the test projects in the same solution or not is up to you. Follow these simple steps to get started unit testing your code. Keep in mind this is by no means a comprehensive unit testing article. If you're reading this, I'm assuming you know what unit tests are and why you should use them.

In this example, we are going to create a console application, add a library and a unit test for that library.

  • Run MonoDevelop.
  • Follow the tutorial here to create the solution, console application, and a library.
  • Add another library to the solution as you did in the tutorial and call it MyLibraryTest.
  • Rename MyLibraryTest->MyClass.cs to MyClassTest.cs.
  • Open MyLibraryTest->MyClassTest.cs and rename the class to MyClassTest. Note: you can easily rename the class by right-clicking on its name and selecting Class->MyClass->Rename.

    RenameClass.png

  • From the MyLibraryTest project, add a reference to the MyLibrary project and a reference to the nunit.framework assembly. In the "Edit References" window, select the "Packages" tab and search for nunit (in Ubuntu, you can just start typing nunit and the window will scroll automatically).
  • Add the following using statements to MyLibraryTest->MyClassTest.cs after the "using System;" statement:
    C#
    using MyLibrary;
    using NUnit.Framework;
  • Add the following attribute to MyClassTest:
    C#
    [TestFixture]
  • Add the following method to MyLibrary->MyClass.cs:
    C#
    public int Divide(int number1, int number2)
    {
       int result;
       try
       {
          result = number1 / number2;
       }
       catch(DivideByZeroException)
       {
          result = -1;
       }
       return result;
    }
  • Add the following method to MyClassTest:

    C#
    [Test]
    public void DivideTest()
    {
       MyClass mc = new MyClass();
       
       int result = mc.Divide(12, 6);
       
       Assert.AreEqual(2, result);
       // 
       // Dividing by zero should result in -1 since the function handles this case.
       result = mc.Divide(0, 22);
       Assert.AreEqual(-1, result);
    }
  • Execute your test:

    • Select View | Unit Tests to bring up the unit tests window.

      ViewUnitTests.jpg

    • Right-click the test in the unit test window and select Run Test.

      RunTest.png

    • Select View | Test Results to view the results.

      ViewTestResults.jpg

    • Test Results

TestResults.png

So in conclusion, it's relatively easy to get started down the path to TDD using NUnit and MonoDevelop. I hope that someone from NUnit and/or MonoDevelop might read this article and say “you nailed it!” or “this is complete rubbish”. Either way I hope you find this article useful enough to get started. Also, any feedback that you can provide is much appreciated. Happy coding!

Background

  • Familiarity with Test Driven Development

Using the Code

Download the zipped code sample from here and open it with MonoDevelop.

Points of Interest

I discovered that MonoDevelop 1.0 doesn't have a built-in debugger. Apparently you can get an addin but I recommend waiting for MonoDevelop 2.0 which is due to be out this year. Here is a list of features.

One thing I noticed on Ubuntu is that MonoDevelop 1.0 crashes a lot. I'm not sure if that has to do with the unit testing I was doing, but I hope that 2.0 is more stable.

I noticed that even though there is a catch for a divide by zero exception, the test still fails. Can you figure out why?

History

  • [3.22.2009 Update]
    • Major changes to include more of the actual solution and test setup
  • [3.19.2009 Update]
    • Found the following related article
  • [3.18.2009 Update]
    • Fixed the code block display
    • Updated source code to split out unit tests to have one per method

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)