Relooking at xUnit.net RC2
Earlier this week, I wrote about the latest release of xUnit.net RC2. Since that time, Brad Wilson and Jim Newkirk released a new version to fix some of the issues with regards to TestDriven.NET integration and ASP.NET MVC integration. You can read more about the issues here. As always you can get the latest bits here.
The Installer
If you browse to the releases page of xUnit.net, you will notice an installer. This installer will help integrate xUnit.net with Resharper 3.1 for Visual Studio 2005 and 2008, TestDriven.NET and ASP.NET MVC. Just click the installer and a screen like below will show. Note that I have Resharper 4.0 nightly builds instead of 3.1, so those features aren't activated just yet, unfortunately.
Since I didn't have anything else but ASP.NET MVC Preview 2 installed, that's all I got enabled. It only takes a few seconds to enable.
ASP.NET MVC
Now that I got the ASP.NET MVC plugin installed for xUnit, I can go ahead and open Visual Studio 2008 and create a new ASP.NET MVC application. When it automatically creates the test project for me, I have the option now of using xUnit.net instead of that lesser one that came in here by default.
Once I do that, it will create some tests of the HomeController and Routes by default as noted below:
But in order for this to work out of the box, there is some work you need to do. Your tests will not compile by default because you need to change the namespace includes on top to include your application's namespace. Not a huge issue mind you, but a little more work than usual. Here's what it will look like:
using MVC_APPLICATION_NAMESPACE.Controllers; // This using directive needs to point to the namespace of your MVC project
using Xunit;
namespace PetShopTests.Controllers
{
public class HomeControllerFacts
{
public class Index
{
[Fact]
public void RendersTheIndexView()
{
TestableHomeController controller = new TestableHomeController();
controller.Index();
Assert.Equal("Index", controller.RenderView_ViewName);
}
}
So, all you need to do is replace the MVC_APPLICATION_NAMESPACE with your own namespace and problem solved! But out of the box, you have some working unit tests and some good guidance on how to get started. If you're curious on how the integration actually works, check out this post here.
GUI Runner
As Brad had stated earlier, there is a simple, yet effective GUI runner for xUnit.net. So far there's not much to it, other than to load an assembly and run all associated tests. To give you an example, I wrote some F# code and now I want to run some tests against it. So, here is the code I used for these simple tests. Yes, you can go ahead and yell at me for doing OO things in a functional language, but it's here to prove a point that you can.
#light
#R @"E:\Tools\xunit-build-1223-samples\Samples\xunit\xunit.dll"
open System
open System.Collections.Generic
open Xunit
type Stack<'t> = class
val elements : LinkedList<'t>
new() = { elements = new LinkedList<'t>() }
member x.IsEmpty
with get() = x.elements.Count = 0
member x.Push element =
x.elements.AddFirst(element:'t)
member x.Top
with get() =
if x.elements.Count = 0 then
raise (InvalidOperationException("cannot top an empty stack"))
x.elements.First.Value
member x.Pop =
let top = x.Top
x.elements.RemoveFirst()
top
end
[<Fact>]
let NoElementsShouldBeEmpty () =
let stack = new Stack<string>()
Assert.True(stack.IsEmpty)
[<Fact>]
let PushAnElementShouldNotBeEmpty() =
let stack = new Stack<string>()
stack.Push("first element")
Assert.False(stack.IsEmpty)
And now, open the xunit.gui.exe and select your assembly you want to run. I chose my fsharpsamples.dll that I've been working with on my F# samples. Now, let's give these two tests a run to see how they do.
As you can see, not much information is given to me other than the number of passed tests, the time taken and my assembly under test. Not much information, but enough to get started.
Conclusion
As you can see, a bit of progress has been made on xUnit.net and here's hoping the version 1.0 full release is coming soon. As I look at it more and more, I'm definitely starting to like it. I haven't had to create as many extensions as I would have to do with MSTest such as Assert.Throws<T> and so on. So, I encourage you to go ahead and once again, give it a look and give feedback to the team. Until next time...