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.

 

 

 

9 Comments

  • 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!

  • 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!

  • well what worked for me is:

    <projectname>.dll.config

    weird!

    thanks though - wouldn't have had a clue otherwise

  • 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: .dll.config

    This thread solved my problem. Thanks!




  • Worked great - Thank you.

  • Thanks for the tip this helped greatly.

  • Hi guys,

    Question on this lines:
    "After following the steps of copying my web.config to my unittest project, I had to use: .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


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

  • .config is right!

    but 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!

Comments have been disabled for this content.