Skip to main content

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.

Comments

Popular posts from this blog

Marshalling strings with StringBuilder

The dotNet framework is pretty smart when it comes to marshalling managed to unmanaged datatypes for PInvoke calls. When you want to pass a Guid from the managed to the unmanaged world the framework knows how to transform the .Net Guid structure to a Windows GUID without you even asking for it.

Agile architecture, embrace change by reducing complexity

Software development can sometimes be very counterintuitive. For example, when people started building larger software systems halfway through the last century one of the things they discovered was that changing existing software is often far more difficult and error prone than building new software. Most developers rediscover this somewhere in their first year of being a professional developer. They start out with a new project and as time goes by changes start taking more and more time, this can be very hard to explain to a client and usually we don’t until we get completely stuck and tell the client we need to start from scratch. I think this practice has been responsible for most of the bad reputation software development has.

Square One available on the Android market

This is just a short post to let you know that a first version of the Android app I’ve been working on for the last couple of weeks is available on the Android market. The app is called Square One and it’s a simple bassline synthesizer. It’s free so try it out and let me know what you think of it, but be prepared it’s still an early version. I hope to add more features in the next few months and maybe build something that can be used to create real music.The lower part of the screen contains the sequencer controls that can be used to program your own bass lines. On the left is a four by four grid of buttons where you can select a step in the sequence. On the right you can select the note to be played on that step. When you’re done you can press Start and the sequence starts playing. The knobs on the top can be used to control a couple of parameters from the synthesizer engine that creates the sound. You can control the cutoff frequency and resonance of the low-pass filter, attack and …