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 18.104.22.168 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->MyClass.cs to MyClassTest.cs.
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:
- Add the following attribute to
- Add the following method to
public int Divide(int number1, int number2)
result = number1 / number2;
result = -1;
Add the following method to
public void DivideTest()
MyClass mc = new MyClass();
int result = mc.Divide(12, 6);
result = mc.Divide(0, 22);
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