Geeks With Blogs
Dave Chestnutt: SparklingCode and CodeGaffes Writing better code; for fun and profit

How to easily run NUnit tests in both .NET 1.1 and 2.0 environments.

 

I want to run my NUnit tests! But I have a problem -- my code runs under both .NET 1.1 and 2.0 frameworks.  It is real easy to run tests from the IDE, which I do, but that means I'm only running the tests under one version of .NET.

 

How can I easily run tests under both .NET frameworks?

 

I'm lazy, so it has to be easy.  So, without even resorting to the command line, here's how I do it in 3 steps:

 

1. Running tests with the .NET 1.1 Framework

I use Visual Studio 2003, and the TestDriven.NET Add-in (http://www.testdriven.net/).  I right-click on tests, projects, or the whole solution - and run my tests under .NET 1.1.

 

2. Running tests with the .NET 2.0 Framework

I use the NUnit GUI app to run tests under the .NET 2.0 environment.  To make the NUnit GUI run with .NET 2.0 framework - I modified the nunit-gui.exe.config file so its startup section has the 2.0 runtime first:

 

<startup>
  <!-- NUnit will run under the first version it finds -->
  <SUPPORTEDRUNTIME version="v2.0.50727"/>
  <SUPPORTEDRUNTIME version="v1.1.4322" />
  <SUPPORTEDRUNTIME version="v1.0.3705" />
  <REQUIREDRUNTIME version="v1.0.3705" />
</startup>

 

I place a shortcut to the NUnit GUI on my Quick Launch toolbar, and I'm all set.  While it's still true that I run the tests in the IDE the most, I can easily start the NUnit GUI and fire off my tests under .NET 2.0.  And since it's easy, I do it.

 

3. Making a test conditional based on .NET framework version

Some of my tests only make sense under .NET 2.0, and some under 1.1.  [They all compile under 1.1 because we support both.]  So how do I make a few tests conditional?  This requires two things: How to detect the version of the framework in use, and how to stop NUnit from running tests that are inappropriate for the framework in use.

 

I've seen a bunch of ways to detect what framework version we're actually running under.  This is my favorite because it's so simple and clear:

 

public bool IsUsingDotNet11()
{
    return (Environment.Version.Major == 1)
        && (Environment.Version.Minor == 1);
}
 
public bool IsUsingDotNet20()
{
    return (Environment.Version.Major == 2)
        && (Environment.Version.Minor == 0);
}

 

As far as NUnit goes, we let it run all the tests, because we add checks inside the tests to stop them if they require the "other" version of the framework.  By using NUnit's Assert.Ignore() method, we can give a nice message, as well as gracefully stop the test in its tracks.

 

[Test]
public void TestSomethingOnDotNet11Only()
{
    if (! IsUsingDotNet11())
    {
        Assert.Ignore("This test is for .NET 1.1 only");
    }
    // … Rest of test code ...
}

 

There you have it.  You can adapt this technique to suit your needs.  For example, if you run your tests from a batch file, you can modify nunit-console.exe.config to make the console run under any version of the Framework that's installed on the machine.

 

The reality is that many of us have to support more than one version of the DotNET framework.  Until such support is built into Visual Studio, we'll have to resort to methods like this to test in the real world.  Hopefully this short cut will make it easy. 

 

So you can be lazy like me.

 

Posted on Tuesday, March 7, 2006 5:44 PM Visual Studio Tips , NUnit Tips | Back to top


Comments on this post: NUnit - support both .NET 1.1 and 2.0 at the same time

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Dave Chestnutt | Powered by: GeeksWithBlogs.net