Click here to Skip to main content
Click here to Skip to main content

Tagged as

Setup a Test Project with NUnit and MonoDevelop

, 24 Mar 2009
Rate this:
Please Sign up or sign in to vote.
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:
    using MyLibrary;
    using NUnit.Framework;
  • Add the following attribute to MyClassTest:
    [TestFixture]
  • Add the following method to MyLibrary->MyClass.cs:
    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:

    [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)

Share

About the Author

Reese
Software Developer (Senior)
United States United States
C# Rules!

Comments and Discussions

 
QuestionAutomatic unit tests on successful assembly/solution build PinmemberJeff Dickey26-Apr-09 8:29 
Environment: OpenSuse 11.1, Mono 2.4, MonoDevelop 2.0.
 
I've been initially successful in porting my test-infectedness to NUnit/MonoDevelop, but...
 
I'd like to be able, in the IDE, to automatically start unit tests for an assembly or solution (if it contains multiple assemblies) on successful compilation. I've been through the settings as closely as I can, and the IRC channels are deeply silent.
 
Any tips? Thanks very very much....
 
Jeff Dickey
Seven Sigma Software and Services
Phone/SMS: +65 8333 4403
Yahoo! IM: jeff_dickey
MSN IM:    jeff_dickey at hotmail.com
QQ IM:     30302349
Skype:     jeff_dickey

AnswerRe: Automatic unit tests on successful assembly/solution build PinmemberReese27-Apr-09 9:58 
GeneralMono's performance is horrible....FYI Pinmemberj105 Rob2-Apr-09 2:20 
GeneralRe: Mono's performance is horrible....FYI PinmemberReese2-Apr-09 4:11 
GeneralRe: Mono's performance is horrible....FYI Pinmemberj105 Rob2-Apr-09 5:07 
GeneralRe: Mono's performance is horrible....FYI PinmemberReese3-Apr-09 4:05 
GeneralRe: Mono's performance is horrible....FYI Pinmemberspoodygoon4-Apr-09 2:42 
GeneralThanks! Pinmemberspoodygoon23-Mar-09 8:55 
GeneralRe: Thanks! PinmemberReese24-Mar-09 12:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 24 Mar 2009
Article Copyright 2009 by Reese
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid