Wednesday, September 12, 2007

One of the disadvantages of using non-mainstream .NET languages, like Chrome, F# and IronPython is that the most of the popular and most useful VS.NET add-ins tend not to support them. I can live with this for most things (although its getting harder as these add-ins get better), however, one thing that I can't live without is not being able to run my unit tests without leaving Visual Studio.

I'm using Chrome in my current project, I also use JetBrains ReSharper. ReSharper currently supports C# and VB.NET (JetBrains say that they have no plans to support Chrome :-( ). Its a marvalous refactoring tool and it also comes with a built in unit test runner*. This built in test runner will allow you run or debug a test by clicking on an icon in the left-hand gutter of the editor. Or, you can run your tests via a dockable test browser window that lists all of your test fixtures and tests in the current solution. As you can imagine, this is really convenient; so much so, that with a little imagination you can almost use VS.NET like an interactive IDE similar to a Smalltalk workspace.

Unfortunately, ReSharper's unit test runner doesn't understand Chrome and this put me on a search to try and find a unit test runner that did. At the very least I was hoping for an add-in that would find the tests in a compiled assembly, like NUnit does, and let me run them all the tests by a key-stroke or mouse click, if not individually. 

I could only find two other in-IDE unit test runners for Visual Studio. Of the two I found I tried TestDriven.NET - nada; then I tried ExactMagic’s TestMatrix - zilch. I wasn't surprise by this and in a way I was sort of relieved, I was reluctant to install yet another VS.NET add-in just to run some tests when I already had a perfectly good add-in that could do just that.

At this point I had a brain wave – perhaps I could run my tests via C#.  To try this I added a C# project to my Solution and created a class in the new C# project. I then added the necessary NUnit assembly and using references to the new project and then marked the new C# class as a TestFixture.  Then I inherited the C# class from by Chrome test fixture class (see below).


using NUnit.Framework; 
namespace GeoMEM.NETandCF.Maths.Tests { 
   [TestFixture] 
   public class GMatrixTestWrapper : GMatrixTests { 
   }
}

The Chrome test fixture is declared as follows:


uses
  GeoMEM.NETandCF.Maths,
  Nunit.framework,
  NUnit.Framework.SyntaxHelpers;
  
type
  [TestFixture]
  GMatrixTests = public class( AssertionHelper )
  ...
  public
  ... Lots of tests ....
  end;

I built the new test wrapper project and then ran the tests via the gutter icon. Sure enough, all my Chrome tests duly appeared in ReSharpers unit test browser (and passed, of course). I can even run tests individually and debug them via the test browser. The only thing I don't get is the ability to run tests straight from the editor, but I can live without that. The other thing that I’ve noticed is that if make changes to either your test code or the code under test you have to make sure that you compile the code via C# test wrapper project. Otherwise the wrapper project doesn’t get recompiled and you end up running “stale” tests.

So, to summarise; if you practice TDD and work in a language other than C# or VB.NET then chances are you can use an in-IDE unit test runner if you create a C# project and derive a C# test fixture from each of other languages test fixtures. You will get virtually all the advantages of a in-IDE test runner with perhaps one or two minor niggles.

*JetBrains also do a freebie unit test runner called UnitRun, you can get from here: http://www.jetbrains.com/unitrun/ and what I’ve described here should also work with UnitRun too.