Keeping NUnit config files in sync

When doing Test-Driven Development with NUnit, you can add an application config file by adding a file called AssemblyName.dll.config to the executing directory. In VB it’s not a problem since the bin directory is it (you could just add the config file to the bin directory and “include in project”), but in C# there is a Debug and a Release subdirectory under bin depending on the Visual Studio solution configuration.


The problem is getting your NUnit config file into those directories without having to maintain two copies. The answer is Build Events, specifically post-build events.


First, add an App.config file to your unit test project. Copy everything from your current AssemblyName.dll.config file into the App.config file. The problem is if your project is a class library, Visual Studio won’t rename the file and copy it to the output directory like it does with a Windows Forms project.


Now right-click on the unit test project, select Properties, and select Build Events. Click the ellipses (…) next to Post-build Event Command Line. This will open up a batch file editor. Click the Macros >> button to see which macros are available to you. It even shows you the values you can expect given your current project configuration.


That was just handy information for your benefit. Here is the post-build event you would use to copy your App.config to the output directory and rename it to AssemblyName.dll.config (copy and paste):


copy /Y “$(ProjectDir)App.config” “$(TargetDir)$(TargetFileName).config”


Now when you rebuild your project, the App.config file will be renamed to AssemblyName.dll.config and copied to the output directory so that NUnit can access it for configuration information. Note that this should work for VB too, in case you want to keep source-controlled files out of your bin directory. Simple!

This entry was posted in TDD. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

10 Responses to Keeping NUnit config files in sync

  1. Jason Meridth says:

    Thank you! This helped us finally put some of our tests into a TDD test harness. Thank you!

  2. Sugashini says:

    This was what I was looking for….
    Thank You !!!!!!!!

  3. Greg says:

    Worked like a charm! .NET doesn’t always do this for you. Also, when testing web apps, you may have to pull the from the web.config into the app.config.

  4. Bob Myknob says:

    Awesome! Just what I was looking for!

    Thanks!

  5. jmmj says:

    NUnit not finding my config files was a piece of my frustrations yesterday. I finally figured out that I should copy the file and edit the options in NUnit. Today I started my RSS aggregator for the first time in a couple of weeks, and there you are with the answer. Perhaps I should keep up with my reading.

  6. darrell says:

    Ayende – Visual Studio only copies/renames the App.config file if the project is an executable (ie, Windows Forms). It does not work for Class Library projects as far as I know.

  7. darrell says:

    Thanks Richard!

  8. Paul Gielens says:

    We only used the copy command where we could have renamed it just as well 😀 Thanks for the tip.

  9. doesn’t vs.net already do it for you?

  10. VB.NET doesn’t support pre or post build rules natively, but there is an add-in to enable this. It’s the PrePostBuildRules add-in available at http://www.microsoft.com/downloads/details.aspx?familyid=3ff9c915-30e5-430e-95b3-621dccd25150&displaylang=en. Scroll down for just this example.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>