TestDriven.NET 2.18 + NUnit 2.5 Beta

I’ve just uploaded a new version of TestDriven.Net with support for NUnit 2.5 Beta.

There’s a menagerie of weird and wonderful new attributes to choose from in this point release of NUnit. Ben Hall has written a good summary of the Alpha version and I’m sure Charlie Poole will be blogging about the Beta in the coming days.

Here’s a quick summary of a few new attributes:

Generic test fixtures can be used when you need to run a batch of tests against few different implementations of a type. In the past something similar could be achieved by having an abstract base fixture and extending it for each implementation you needed to test. By using a generic fixture you keep all your test code in a single class and I think it’s more expressive.

    [TestFixture(typeof(ArrayList))]
    [TestFixture(typeof(List<int>))]
    public class IListTests<TList> where TList : IList, new()
    {
        [Test]
        public void Count()
        {
            IList list = new TList { 1, 2, 3 };
            Assert.AreEqual(3, list.Count);
        }
    }

The ‘TestCase’ attribute is similar to MbUnit’s ‘RowTest’. With this attribute you transform a single test method into multiple test cases. You can also define an expected return result, but I’d advise against using this if you want a stack trace when your test fails. It’s better to explicitly define the assert inside the test method.

    public class TestCases
    {
        [TestCase(4, 2, 2)]
        [TestCase(2, 1, 1)]
        [TestCase(5, 2, 3)]
        public void Add(int answer, int a, int b)
        {
            Assert.AreEqual(answer, a + b);
        }
    }

In previous versions of NUnit you could specify which threading model your tests required by adding some XML to your test project’s App.config file. You can now specify this directly on the test that needs it using the RequiresMTA/STA attributes.

    [Test, RequiresMTA]
    public void MTA()
    {
        Assert.AreEqual(ApartmentState.MTA,
            Thread.CurrentThread.ApartmentState);
    }
    [Test, RequiresSTA]
    public void STA()
    {
        Assert.AreEqual(ApartmentState.STA,
            Thread.CurrentThread.ApartmentState);
    }

Lastly the ‘TestFixture’ attribute is no longer required and test methods are allowed to be static. This means NUnit can now be used in a natural way for testing F# code. Note, you will need to have "Other Flags" set to "--optimize+ notailcalls" in your project’s build properties if you want to see a stack trace on any failed asserts.

    #light
    open NUnit.Framework
    [<Test>]
    let fsharp() = 
        Assert.AreEqual(2 + 2, 4)

Update: I’ve tried to highlight a few features in NUnit 2.5 which can be used to make your unit tests clearer. Xerxes Battiwalla has written a post about Assert.Throws<T>() which also falls into this category.

    [Test]
    public void CreateDomain_Null()
    {
        Assert.Throws<ArgumentNullException>(() => AppDomain.CreateDomain(null));
    }

For more information see the TestDriven.Net 2.18 release notes and the NUnit 2.5 documentation.

Published Tuesday, December 2, 2008 5:15 PM by Jamie Cansdale

Comments

# re: TestDriven.NET 2.18 + NUnit 2.5 Beta

Hi

I am geting this error when I try to execute unit tests. Can you tell me what am I doing wrong?

Wednesday, December 9, 2009 5:04 PM by ERROR IN TEST RUNNER: NUnit 2.5 === System.Xml.XmlException: Root element is missing

# re: TestDriven.NET 2.18 + NUnit 2.5 Beta

Try checking that your test project's App.config file contains valid XML.

Wednesday, December 9, 2009 5:49 PM by Jamie Cansdale

# re: TestDriven.NET 2.18 + NUnit 2.5 Beta

Testdriven net 2 18 nunit 2 5 beta.. Nifty :)

Sunday, May 1, 2011 12:44 PM by weblogs.asp.net

# re: TestDriven.NET 2.18 + NUnit 2.5 Beta

Testdriven net 2 18 nunit 2 5 beta.. Awesome :)

Tuesday, June 14, 2011 4:09 PM by weblogs.asp.net