Friday, September 21, 2007
James Newkirk and Brad Wilson have cooked up a new testing framework for .NET development: xUnit.net.

In his blog post announcing xUnit.net, James outlined the reasons why they have created a new framework -- primarily to bake best practice techniques into the test framework, and leverage some of the newer .NET features.

The Comparison between Nunit, MSTest and xUnit.net page on the site lists some of the differences between xUnit.net and Nunit / MSTest. Removing setup and teardown, and providing aspect-like functionality are both interesting angles which I want to spend some time with.

Here's a couple of examples from the samples download. First, an example of usage of BeforeAfterTestAttributes, which let you add behaviour before and after the execution of a test (ala AOP):

[Test, TracingSplicer]
public void TestThis()
{
}

...

public class TracingSplicerAttribute : BeforeAfterTestAttribute
{
    public override void Before(MethodInfo methodUnderTest)
    {
        Console.WriteLine("Before : {0}.{1}",
            methodUnderTest.DeclaringType.FullName, methodUnderTest.Name);
    }

    public override void After(MethodInfo methodUnderTest)
    {
        Console.WriteLine("After : {0}.{1}",
            methodUnderTest.DeclaringType.FullName, methodUnderTest.Name);
    }
}


Here's an example of test method extensibility:

[RepeatTest(5, Timeout=500)]
public void RepeatingTestMethod()

You can create your own attributes that inherit from the TestAttribute base class, and define what will happen when the test runs (in this example, the test will run 5 times).


Good work guys, looks useful!

Kirk
All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (HTML not allowed)