CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Darrell Norton's Blog [MVP]

Fill in description here...

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!


Published May 13 2005, 08:39 AM by darrell
Filed under:

Comments

Richard Dudley said:

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.
# May 13, 2005 9:04 AM

Ayende Rahien said:

doesn't vs.net already do it for you?
# May 13, 2005 12:17 PM

Paul Gielens said:

We only used the copy command where we could have renamed it just as well :D Thanks for the tip.
# May 14, 2005 12:50 AM

darrell said:

Thanks Richard!
# May 16, 2005 10:50 AM

darrell said:

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.
# May 16, 2005 10:51 AM

Loren Halvorson's Blog said:

# May 20, 2005 11:07 AM

peterMarshall said:

NUnit config
# May 25, 2005 4:49 AM

jmmj said:

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.
# May 25, 2005 6:53 AM

Bob Myknob said:

Awesome! Just what I was looking for!

Thanks!
# May 26, 2005 8:48 AM

Jeff Perrin said:

# June 2, 2005 1:19 PM

Greg said:

Worked like a charm! .NET doesn't always do this for you. Also, when testing web apps, you may have to pull the <appSettings> from the web.config into the app.config.
# June 28, 2005 10:57 AM

Sugashini said:

This was what I was looking for....
Thank You !!!!!!!!
# August 3, 2005 3:07 AM

Jason Meridth said:

Thank you! This helped us finally put some of our tests into a TDD test harness. Thank you!
# August 10, 2005 12:34 PM

mattonsoftware.com said:

The following links to .NET resources have been collated over time with the assistance of colleagues.&amp;nbsp;...
# May 6, 2006 4:30 AM
Check out Devlicio.us!