Paul On Technology

Exploring technology

NUnit tip for ConfigurationSetings.AppSettings

Today I had to unit test a dll that will read the web.config file from my web app. Here is the setup

     MyUnitTester

           TestSettings (Tests GetSettingsFromKey)

     MyBusinessObject

           GetSettingsFromKey

I never had to do this before so I read the NUnit PDF and it actually turned out to be quite simple.

  • Copy the web.config file that you will read into the bin\debug directory of the test project.
  • Look for a file with the .nunit extension (should be the same name as the test project's dll)
  • Rename the web.config file to the same name as the nunit file but change .nunit to .config.
  • Run your tests.

 

 

 

Comments

TrackBack said:

# May 31, 2004 10:57 PM

Jeff said:

Didn't work for me. In fact, I put this in a test, and it always outputs a zero:

Console.WriteLine(ConfigurationSettings.AppSettings.Count.ToString());

I'm pulling my hair out here!
# May 31, 2004 11:05 PM

Jeff said:

In the NUnit build I have, turns out the config file needs to be called <nunitproject>.nunit.config. Not what anyone really expected, but it works!
# May 31, 2004 11:27 PM

steven said:

well what worked for me is:
<projectname>.dll.config
weird!
thanks though - wouldn't have had a clue otherwise
# June 4, 2004 10:52 AM

TrackBack said:

# June 21, 2004 12:09 PM

TrackBack said:

# August 15, 2004 11:16 AM

TrackBack said:

# August 17, 2004 9:51 PM

mike bouchard said:

I'm using nunit 2.4.1 and I couldn't get a connectionstring.

After following the steps of copying my web.config to my unittest project, I had to use: <projectname>.dll.config

This thread solved my problem. Thanks!

# July 10, 2007 3:03 PM

Joe said:

Worked great - Thank you.

# August 28, 2007 1:53 PM

KelDawg said:

Thanks for the tip this helped greatly.

# November 27, 2007 7:08 PM

Dharyl said:

Hi guys,

Question on this lines:

"After following the steps of copying my web.config to my unittest project, I had to use: <projectname>.dll.config"

Question: What projectname are you talking about? is it the unittest project name? or the project name of the project(dll) to be tested?

Thanks,

dha

# August 4, 2008 11:43 PM

Dharyl said:

I made it work. It's like magic! Thanks to this thread!

And to clarify confusions:

its:

<TestProject_name.dll>.config - disregard the <>

You just have to copy the web.config file and paste it inside your test project. Then change the <web>.config to <TestProject_name.dll>.config. So if your project name is NUnitLibrary, then the dll file must be NUnitLibrary.dll , so the config file should be NUnitLibrary.dll.config. Then everything will work like magic!

Thanks!

dha

dharylcauzon@hotmail.com

# August 5, 2008 3:04 AM

Tom said:

I believe I've tried all of these suggestions and it's still not working for me.  I have a project that's a c# dll, with a single test in it.  To that project I've added a settings file which, when built produces a file named <projectname>.dll.config alongside the dll.

That seems to be right, no?  But every time I try to read it ConfigurationSettings.AppSettings.Count is zero.  If I totally mess up the format of the <projectname>.dll.config file I can get it to crash when the test runs, but when I "fix" it back to the generated one, interrogating it always comes up empty for values.  So far it has been pretty frustrating.  Has anyone experienced this?

# September 24, 2008 1:55 PM

Sachin said:

Rename web.config file to <projectname>.dll.config  after you have copied to /bin/debug on the test project.

# January 18, 2010 1:52 PM

zhangxp said:

<projectname>.config is right!

but <projectname> maybe equals to "TestProject_name.dll" or "TestProject_name".

it is dependent on NUNIT.exe,which opens(File->Open Project...) .dll or .csproj is defferrent!

# May 15, 2010 3:00 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)