Unit Testing with NUnit and Moles Redux

Almost two years ago, when Moles was still being packaged alongside Pex, I wrote a post on how to run NUnit tests supporting moled types.

A lot has changed since then and Moles is now being distributed independently of Pex, but maintaining support for integration with NUnit and other testing frameworks.

For NUnit the support is provided by an addin class library (Microsoft.Moles.NUnit.dll) that you need to reference in your test project so that you can decorate yours tests with the MoledAttribute. The addin DLL must also be placed in the addins folder inside the NUnit installation directory.

There is however a downside, since Moles and NUnit follow a different release cycle and the addin DLL must be built against a specific NUnit version, you may find that the release included with the latest version of Moles does not work with your version of NUnit.

Fortunately the code for building the NUnit addin is supplied in the archive (moles.samples.zip) that you can found in the Documentation folder inside the Moles installation directory. By rebuilding the addin against your specific version of NUnit you are able to support any version.

Also to note that in Moles 0.94.51023.0 the addin code did not support the use of TestCaseAttribute in your moled tests. However, if you need this support, you need to make just a couple of changes.

Change the ITestDecorator.Decorate method in the MolesAddin class:

Test ITestDecorator.Decorate(Test test, MemberInfo member)
{
    SafeDebug.AssumeNotNull(test, "test");
    SafeDebug.AssumeNotNull(member, "member");

    bool isTestFixture = true;
    isTestFixture &= test.IsSuite;
    isTestFixture &= test.FixtureType != null;

    bool hasMoledAttribute = true;
    hasMoledAttribute &= !SafeArray.IsNullOrEmpty(
        member.GetCustomAttributes(typeof(MoledAttribute), false));

    if (!isTestFixture && hasMoledAttribute)
    {
        return new MoledTest(test);
    }

    return test;
}

Change the Tests property in the MoledTest class:

public override System.Collections.IList Tests
{
    get
    {
        if (this.test.Tests == null)
        {
            return null;
        }

        var moled = new List<Test>(this.test.Tests.Count);

        foreach (var test in this.test.Tests)
        {
            moled.Add(new MoledTest((Test)test));
        }

        return moled;
    }
}

Disclaimer: I only tested this implementation against NUnit 2.5.10.11092 version.

Finally you just need to run the NUnit console runner through the Moles runner. A quick example follows:

moles.runner.exe [Tests.dll] /r:nunit-console.exe /x86 /args:[NUnitArgument1] /args:[NUnitArgument2]
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s