Tuesday 28 December 2010

Convert VS Tests to NUnit

I've got a small (well, not that small really) .Net library with some utilities ready to be included in other projects (StringHelpers, some Reflection and Logging stuff...).
It's been mainly stopped for the last year, but the other day needed to go back to some code that I had there and realized it would be a good idea to update it to .Net 4 (even when the only improvement that I'm aware of being using in my new code is the Generic variance one).

For different reasons... I still don't have Visual Studio 2010 installed, so I'll also switch the development to SharpDevelop 4. That should not be a problem, as for libraries or console applications SharpDevelop has little to envy.

Problem is that my tests for this library are in a Visual Studio Test Project. SharpDevelop can compile it, but it can't run it, so the way to go is to jump to NUnit. You have to manually transform the Tests from one format to another, but thanks to this Article (many thanks man, it's been pretty useful to me) it's pretty simple.

Team System NUnit
using Microsoft.VisualStudio.TestTools.UnitTesting; using NUnit.Framework;
[TestClass] [TestFixture]
[TestMethod] [Test]
[TestInitialize] [SetUp]
[TestCleanup] [TearDown]


It's really amazing that both test systems have an Assert class that seems to have more or less (I haven't checked the source, just my test conditions worked fine without changing anything) the same methods.

So, this is what you need:

  • Install NUnit (it does not come installed by SharpDevelop)

  • Do all the renaming mentioned in the article

  • Now, you can do the GUID replace explained in the article to change a test project to a class library. I didn't need it cause I preferred to keep a copy of the VS Test Project, so I removed it from the Solution and created a new Library project where I duplicated the code files

  • Go to View -> Tools -> UnitTest
    this will open a NUnit Panel. From there you can click the "Add reference to NUnit to the currently selected project". And that's all. Now you can run your tests from inside SharpDevelop or launch the NUnit GUI, open the Assembly containing your tests and run them from there.


I quite like this, it seems like you're a bit more free with the NUnit approach. Now your tests can be run from the NUnit GUI, from SharpDevelop, and even from Visual Studio.

No comments:

Post a Comment