How to run NUnit tests created with VS2010 and .NET 4

Today when writing tests for a new project built using VS2010 I had an interesting problem:

NUnit would not run my unit tests because the assembly that contained my unit tests was compiled using a newer version of the .NET runtime.

There are several solutions to solve this issue from downgrading my project to use earlier version of .NET to migrate my unit tests to MSTest but luckily for me no such drastic move was needed.

The new NUnit (2.5.x) have support for running tests using .NET 4 it’s just hidden inside a pile of xml.

The solution

To run .NET 4.0 test assembly add the lines below to the relevant config file, so if you’re using NUnit.exe update nunit.exe.config or if you’re using nunit-console.exe use nunit-console.exe.config – you get the point.

  1. Under <configuration> add:
    <startup><requiredRuntime version="v4.0.21006" /></startup>
  2. Under <runtime> add:
    <loadFromRemoteSources enabled="true" />

I’m using the latest VS2010 RC2 If you have a different version of Visual Studio installed change the version value to the version of the .NET 4 installed on your machine – look at C:\Windows\Microsoft.NET\Framework to find out which version you have.


That’s it!

Labels: , , ,