Darrell Norton's Blog [MVP]

Sponsors

The Lounge

News

  • Darrell Norton pic

    MVP logo

    View Darrell Norton's profile on LinkedIn

    Currently Reading:

    weewar.com

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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!


Posted 05-13-2005 8:39 AM by Darrell Norton
Filed under:

[Advertisement]

Comments

Richard Dudley wrote re: Keeping NUnit config files in sync
on 05-13-2005 9:04 AM
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.
Ayende Rahien wrote re: Keeping NUnit config files in sync
on 05-13-2005 12:17 PM
doesn't vs.net already do it for you?
Paul Gielens wrote re: Keeping NUnit config files in sync
on 05-14-2005 12:50 AM
We only used the copy command where we could have renamed it just as well :D Thanks for the tip.
Darrell Norton wrote re: Keeping NUnit config files in sync
on 05-16-2005 10:50 AM
Thanks Richard!
Darrell Norton wrote re: Keeping NUnit config files in sync
on 05-16-2005 10:51 AM
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.
Loren Halvorson's Blog wrote re: Config files and NUnit tests
on 05-20-2005 11:07 AM
peterMarshall wrote NUnit config
on 05-25-2005 4:49 AM
NUnit config
jmmj wrote re: Keeping NUnit config files in sync
on 05-25-2005 6:53 AM
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.
Bob Myknob wrote re: Keeping NUnit config files in sync
on 05-26-2005 8:48 AM
Awesome! Just what I was looking for!

Thanks!
Jeff Perrin wrote NUnit and App.config Files
on 06-02-2005 1:19 PM
Greg wrote re: Keeping NUnit config files in sync
on 06-28-2005 10:57 AM
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.
Sugashini wrote re: Keeping NUnit config files in sync
on 08-03-2005 3:07 AM
This was what I was looking for....
Thank You !!!!!!!!
Jason Meridth wrote re: Keeping NUnit config files in sync
on 08-10-2005 12:34 PM
Thank you! This helped us finally put some of our tests into a TDD test harness. Thank you!
mattonsoftware.com wrote .NET Resources
on 05-06-2006 4:30 AM
The following links to .NET resources have been collated over time with the assistance of colleagues.&amp;nbsp;...
Webdevelopment Blog » NUnit config wrote Webdevelopment Blog &raquo; NUnit config
on 11-08-2006 5:40 PM
Webdevelopment Blog » NUnit config wrote Webdevelopment Blog &raquo; NUnit config
on 12-07-2006 2:01 PM