OT: NUnit, CSLA BOs and App.Config

OT: NUnit, CSLA BOs and App.Config

Old forum URL: forums.lhotka.net/forums/t/1218.aspx


JHurrell posted on Thursday, September 14, 2006

I've been getting into NUnit and just created some new CSLA (1.53) BOs that I wanted to test. Within my solution, I created a new class library project to store my test fixtures. I added references to CSLA, my CSLA BO library and NUnit.Framework. It follows this pattern:

Solution (Visual Studio 2003)
    Website
    ClassLibrary
    ClassLibrary.Tests

Within ClassLibrary.Tests, I create some fixtures and tests, but when I attempted to run the tests, I get a DataPortal Exception. Oh yeah, there's no .config file for the connection string. I add App.Config and make sure that <appSettings> exists with the connection string.

I attempt to run the tests again and it still fails. Turns out, a class library completely ignores App.Config. In fact, it doesn't even get copied to the Debug folder. Even if I manually copy it there, it is quietly ignored.

So, how the heck do I use NUnit to run unit tests against my CSLA Business Objects? What is everyone else doing to run NUnit?

- John


ajj3085 replied on Thursday, September 14, 2006

You have to tell Nunit explicity to use the configuration file.  Here's how I do it (this a nant project)

        <nunit2>
            <formatter usefile="false" type="Xml" />
            <test
                appconfig="${LocalSolutionBin}\${SolutionName}.Test.dll.config"
            >
                <assemblies>
                    <include name="${LocalSolutionBin}\${SolutionName}.Test.dll" />
                </assemblies>
            </test>
        </nunit2>

There's a command line switch, or you can specify it in the gui as well.

JHurrell replied on Thursday, September 14, 2006

Since I don't use NAnt, at least not yet, I had to dig a little deeper. The UI tip helped and I figured it out. If you open an NUnit Test Project File and click Project >> Edit, there's an entry for Configuration File Name. In the textbox, I simply entered App.Config.

If you look at the .nunit file, it enters the config file as an attribute:

<NUnitProject>
  <Settings activeconfig="Debug" />
  <Config name="Debug" configfile="App.config" binpathtype="Auto">
    <assembly path="bin\Debug\IPSClassLibrary.Tests.dll" />
  </Config>
</NUnitProject>

Thanks for the tip.

- John

Brian Criswell replied on Thursday, September 14, 2006

When you move to VS2005, .dlls can have config files.

BBM replied on Thursday, March 01, 2007

Hi John,

Sorry to be so late to this party.

I usually put an NUnit project in the same Solution as the app that I'm coding.  As an example, lets say that I call that project NUnitProject.

Under this project in the  Bin/Debug folder, place a copy of the app.config file that you want NUnit to use.  Rename this file (using my example project name above) NUnitProject.dll.config

NUnit will now use this file as the app.config file for the application being tested.

Don't forget to install NUnit as an external tool in VS.  Makes it very easy to use NUnit from within VS.

Another tip.  If you're using a local "remote" Dataportal (i.e. you're using remoting, but the app server site is on your dev machine) you can de-bug into the CSLA DataPortal code by going into the Debug menu in VS and selecting "Attach to Process".  The process to attach to is the ASP worker process aspnet_wp.exe ( I learned this from Andy on this forum - Thanks!).

Finally, to debug code in your NUnit test classes, Attach to to the process called nunit_gui.exe AFTER you have started NUnit, but before you have run any tests.  This will allow you to use de-bug breakpoints in your Nunit test methods.

Good luck.

BBM

zythra replied on Thursday, March 01, 2007

I do mine similar to BBM, but take it just a little further.  I use a post build event in my test project to copy a common app.config from another project in my solution and rename it using the post build as well.  This way if my app.config ever changes, my test project will always have a current copy which is especially helpful if your test project relies on any of the settings in your app.config.

I also use the NUnit add-in from www.testdriven.net to run tests from within my IDE, that way I don't have to attach to another process.

Just my two cents, maybe it will be helpful to someone.  :-)

Copyright (c) Marimer LLC