I just started using xUnit.Net for a while now. It’s just a tiny bit cleaner and slightly less abrasive than other .Net unit testing frameworks. I  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?


Creating tests is as easy as it normally is, You have to just reference the xUnit.dll it in your 4.0 test-projects , which you do using Nuget Package manager. 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. So whenever you will try to run your test cases with the runner available with xUnit.Net package (current is 1.9.1) on codeplex or some other source , you will get error.

Solution:

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;
 
 <configuration>   
  <startup>  
      <requiredRuntime version="v4.0.20506" safemode="true"/>  
    </startup>   
  </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 section and it will load 4.0 test-assemblies. You can just copy - paste the xUnit.console.exe.config file and rename it to xUnit.gui.exe.config and do the appropriate changes as mentioned above.


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 aversion for the latest beta available.

0 comments:

Post a Comment

 
Top