Scott Hanselman

Getting NAnt 0.85 to get along with NUnit 2.2

December 28, '05 Comments [3] Posted in NUnit | Nant | XML
Sponsored By

This wasn't obvious to Patrick and I, but it is in retrospect. When you're using NAnt's NUnit2 tasks to run NUnit tests, all the assembly versions need to line up. However, sometimes the version of your test framework might not match up with the version that NAnt was built with. For example, you might have tests that were compiled (and, as such, reference) NUnit 2.0.6 or 2.1.4. However, you're using NAnt 0.85 which runs tests with NUnit 2.2.0.0. Not only that, but the NUnit test runner creates a new AppDomain for each test. That means you can't do a binding (version) redirect with a NAnt.exe.config, because the AppDomain gets a funky name of its own and has its own binding redirect rules that the loader follows.

So, you have to create a test.config file. You can name it anything you want, like mydefaulttest.config, and it'll look something like this:

<configuration>
    ...
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="nunit.framework" 
publicKeyToken="96d09a1eb7f44a77"
culture="Neutral" /> <bindingRedirect oldVersion="2.0.6.0" newVersion="2.2.0.0" /> <bindingRedirect oldVersion="2.1.4.0" newVersion="2.2.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> ... </configuration>

Then you need to TELL the NUnit2 NAnt task that you want to use this specific config file:

<nunit2>
    <formatter type="Plain" />
    <test assemblyname="MyProject.Tests.dll" 
appconfig="mydefaulttest.config" /> </nunit2>

Then the NUnit2 task will make sure the newly created AppDomain uses (or promotes) the correct version and your tests won't fail to load.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Thursday, December 29, 2005 6:46:59 AM UTC
Or use the exec task to fire up nunit-console.exe and save yourself the headache of trying to keep nant and nunit forever in synch.

(Credit to Mike Roberts for asserting this approach as nothing to be ashamed of http://mikeroberts.thoughtworks.net/blog/archive/Tech/ArticlesandPapers/Howtosetupa.NETDevelopmentTree.html)
Thursday, December 29, 2005 8:18:33 PM UTC
Scott:

I totally agree with the previous poster about the exec task. We had to switch to that because the NAnt nunit2 task doesn't make use of (or expose) the /thread option. We're testing some classes that use WaitHandle instances, and that put the kibash on it.

This was earlier in the year and I put in a request for this to be changed in NAnt, so it could've been fixed.

Nice that it works for you, tho.
Dave Purrington
Friday, January 06, 2006 11:34:43 PM UTC
Do you know if this applies to the other direction? My tests are built with the 2.2.5.0 NUnit framework.

I tried putting "2.2.5.0" into "oldVersion", but that didn't seem to work.

I may have to go with the exec task method, which I have working. But I can't figure out how to loop over all the dll's in my build directory. I wish there would more example NAnt build files out there!
Ben Tsai
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.