Click here to Skip to main content
13,249,813 members (77,210 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked
Posted 23 Mar 2009

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


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 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.


  • 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:
  • Add the following method to MyLibrary->MyClass.cs:
    public int Divide(int number1, int number2)
       int result;
          result = number1 / number2;
          result = -1;
       return result;
  • Add the following method to MyClassTest:

    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.


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


    • Select View | Test Results to view the results.


    • Test Results


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!


  • 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?


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


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


About the Author

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

You may also be interested in...


Comments and Discussions

QuestionCannot find reference to NUnit.Framework Pin
Member 1234046220-Feb-16 9:05
memberMember 1234046220-Feb-16 9:05 
QuestionAdding NUnit Support to MonoDevelop on Ubuntu Pin
yfisaqt9-Jul-15 9:11
memberyfisaqt9-Jul-15 9:11 
QuestionAutomatic unit tests on successful assembly/solution build Pin
Jeff Dickey26-Apr-09 9:29
memberJeff Dickey26-Apr-09 9:29 
AnswerRe: Automatic unit tests on successful assembly/solution build Pin
Reese27-Apr-09 10:58
memberReese27-Apr-09 10:58 
GeneralMono's performance is horrible....FYI Pin
j105 Rob2-Apr-09 3:20
memberj105 Rob2-Apr-09 3:20 
GeneralRe: Mono's performance is horrible....FYI Pin
Reese2-Apr-09 5:11
memberReese2-Apr-09 5:11 
j105 Rob, I can't say I've done any performance testing but I can say that your statement is too general. My guess is that you are comparing Mono to the .NET Framework on Windows since .NET Framework doesn't run on Linux and you are probably correct. I think at this point the goal is interoperability plus I imagine that .NET Framework will always run faster on Windows since Microsoft uses "magic" APIs.

"Would anyone tell me if I was getting stupider?"

GeneralRe: Mono's performance is horrible....FYI Pin
j105 Rob2-Apr-09 6:07
memberj105 Rob2-Apr-09 6:07 
GeneralRe: Mono's performance is horrible....FYI Pin
Reese3-Apr-09 5:05
memberReese3-Apr-09 5:05 
GeneralRe: Mono's performance is horrible....FYI Pin
spoodygoon4-Apr-09 3:42
memberspoodygoon4-Apr-09 3:42 
GeneralThanks! Pin
spoodygoon23-Mar-09 9:55
memberspoodygoon23-Mar-09 9:55 
GeneralRe: Thanks! Pin
Reese24-Mar-09 13:07
memberReese24-Mar-09 13:07 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 24 Mar 2009
Article Copyright 2009 by Reese
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid