January 19, 2010

Using xUnit.Net with .Net 4.0

I’ve been using xUnit.Net for a while now. It’s just a tiny bit cleaner and slightly less abrasive than other .Net unit testing frameworks. Leaving out unnecessary stuff like [TestFixture] and shortening Assert.AreEqual to the equally clear but shorter Assert.Equal don’t seem like big improvements but when you type them several times a day tiny improvements start to add up. I also like the use of the [Fact] attribute instead of [Test]. It shifts the focus from testing to defining behavior. So how do we get all this goodness working with the Visual Studio 2010 beta?

Creating tests is as easy as it normally is, you can just reference xunit.dll it in your 4.0 test-projects. Running your 4.0 tests can be a problem, the testrunners included in the xUnit.net package are compiled against the 2.0 framework so they won’t load 4.0 test-assemblies.

The obvious solution is to just download the source and compile it yourself. (+1 for open source!) Make sure you select the .Net Framework 4 and not the .Net Framework 4 Client profile in the project properties for the console runner because it uses some logging functionality that’s not available in the client profile. Everything will just run after this.

I found an easier solution on Mark Needhams blog In his article xUnit.NET: Running tests written in Visual Studio 2010 he explains how you can run the 2.0 console runner with the 4.0 clr by adding a section to the .config file instead of recompiling the whole thing. Adding the following lines to the xunit.console.exe.config should do the trick;

   1: <configuration>
   2: ...
   3:     <startup>
   4:         <requiredRuntime version="v4.0.20506" safemode="true"/>
   5:     </startup>
   6: ...
   7: </configuration>

 

I found the same trick works with the gui runner too. Unlike the console runner it doesn’t come with a config file so you need to create one yourself, add the <startup> section and it will load 4.0 test-assemblies.

 

When you want better VS.Net integration there are some other testrunners you can use. The easiest here is good old TestDriven.Net. You can download the version 3 beta for Visual Studio 2010 integration and run your 4.0 tests with it.

 

Another testrunner that works is the one included with Resharper. Normally it isn’t free but Jetbrains has released a public beta for version 5 that works with Visual Studio 2010. To get xUnit.Net working with Resharper you need the xUnit.Net contrib project. The maintainers have done a great job keeping it up to date with Resharper 5.0 alpha and beta builds so they’ve got a version for the latest beta available.

No comments:

Post a Comment