simoneb's blog

about design, development, profession and avoidance of fluff

NUnit via LINQPad

I’ve been a frequent user of LINQPad for several years now for I enjoy the simplicity with which you can fire it up, start writing code and see the results immediately. I appreciate that other programming languages provide REPLs for you to try and even modify the currently running application, and I enjoy them when I have a chance to work with node.js or Ruby, but this is not something that people writing C# code are used to, and in any case outside the scope of this post.

Recently my involvement with NUnit has increased to some extent and on several occasions I found myself needing to try running one-off tests and quickly see their outcome in order to validate bug reports or in general for experimenting.
As you can guess this would usually require launching a full-featured IDE like Visual Studio, create a class library project, reference NUnit, build and run some NUnit runner against the resulting assembly. That’s a lot of work for just getting a few lines of code to run.

The idea was therefore to be able to run NUnit tests on LINQPad, which is definitely possible and quite simple, as it turns out.

Now, a bit of background about NUnit is in order. The current release, which at the time of this writing is 2.6.2 and has been available for a few days, along with the whole 2.x series, has always kept the framework and the core separate.

The framework is contained in the nunit.framework.dll assembly, which client code usually references in order to write tests. Just to mention a few it contains the TestAttribute and the Assert classes, which everyone writing unit tests should be familiar with.
The core, on the other hand, contains the code needed to run the tests, which includes test discovery, filtering, the logic for handling application domains and processes, and finally exposes this functionality by means of test runners. NUnit currently ships with a console and a graphical GUI runner.
This net separation of the core from the framework basically means that running tests in LINQPad via NUnit requires referencing both the core and the framework assemblies, and then invoking a runner pointing it at the current assembly. This is possible but also not the fastest and cleanest way to accomplish it.

Enter NUnitLite. NUnitLite has been around for some time now and the main difference with NUnit v2 that is of some relevance here is that there is no distinction between the core and the framework. Everything is contained within a single assembly that you can reference from your tests, and the very same project containing the tests can be used to run them with a single one-liner.

Although NUnitLite does not provide all of the features of NUnit it has quite enough of them for most needs, and above all simplifies our life a lot here.
On the other side, we’re going to leverage a new feature available in LINQPad, the ability to reference NuGet packages, which right now is provided in the beta version downloadable from here

Now that the ground is set here are the steps to get started writing unit tests in LINQPad:

  1. Create a new query and set its Language to C# Program, which will create a stub main method
  2. Add the NUnitLite NuGet package, which is easily done by hitting F4, then Add NuGet… and then looking up and selecting NUnitLite. Also add the namespaces NUnit.Framework and NUnitLite.Runner
  3. Fill the main method with the single line which will allow to run the tests in the current assembly and finally start writing unit tests.

    NUnitLite in LINQPad (linqpad-nunitlite.cs) download
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    void Main()
    {
      new NUnitLite.Runner.TextUI().Execute(new[]{"-noheader"});
    }
    
    // Define other methods and classes here
    [Test]
    public void SomeTest()
    {
      Assert.Pass();
    }
    
  4. Hit F5 and see the results of the test run

The steps outlined above are quick and simple, but still require some time if you have to do it over and over, therefore a better option would be to save the raw template as a LINQPad query somewhere on your file system, or even better, although this chance is limited to the owners of a professional license, using a snippet which is available for download from the NUnit wiki here.

The snippet takes care of the whole plumbing so you just need to download and copy it to the snippets folder, usually in %userprofile%\Documents\LINQPad Snippets, then just type the snippet shortcut, nunit, in any query window and you’re ready to write your tests.

Comments