Question

I have written a NUnit tests for a .NET application. When I run the NUnit, it does not read the connection string values from the configuration file. I tried many solutions with out success, like

  1. Adding <assembly name>.dll.config file in the path where NUnit loads the DLL file.
  2. Adding the configuration settings in NUnit.exe.config/NUnit.gui.config

I wasn't able to read the configuration setting even when run in VSNunit. Is there a solution?

Was it helpful?

Solution

I've assumed

  • Assembly being tested: SomeNameSpace.MyClassLib
  • NUnit assembly with unit tests: SomeNameSpace.MyClassLib.Test

Try this:

  1. Make sure that you have also copied your app.config to your NUnit Test DLL class library (i.e. project SomeNameSpace.MyClassLib.Test) as well.

  2. Build your NUnit Project (e.g. to SomeNameSpace.MyClassLib/bin/debug) and make sure that following are in the bin\debug (or release) directory

    • the assembly to be tested,
    • the NUnit test DLL and
    • the configuration (SomeNameSpace.MyClassLib.Test.config)
    • any other assemblies needed by your DLL file being tested.
  3. Edit your NUnit Project in the XML view of the NUnit GUI Project editor (menu ProjectEdit, or just edit it in Notepad), and make sure that the test assembly (MyClassLib.Test.dll) and the configuration file names are relative to your appbase

    For example,

    <NUnitProject>
      <Settings activeconfig="Debug" processModel="Default"
                domainUsage="Default"
                appbase="C:\Temp\MyProject\MyClassLib.Test" />
      <Config name="Debug" binpathtype="Auto"
              configfile="bin\Debug\MyClassLib.Test.dll.config">
        <assembly path="bin\Debug\MyClassLib.Test.dll" />
      </Config>
      <Config name="Release" binpathtype="Auto" />
    </NUnitProject>
    

OTHER TIPS

Add an app.config file to the test project and add your configurations in there.

You then have to tell NUnit what configuration to use as by default it will not pick up the app.config file.

More information on how to set this up with screenshot.

I was stuck on a similar issue for a while. We also need to look at how you are loading the assemblies, based on that the naming of configuration file changes (unless you are using an explicit configuration file from settings). As mentioned here:

http://www.nunit.org/index.php?p=configFiles&r=2.2.10

If a single assembly is being loaded, then the configuration file is given the name of the assembly file with the config extension. For example, the configuration file used to run nunit.tests.dll must be named nunit.tests.dll.config and located in the same directory as the DLL file.

If an NUnit project is being loaded, the configuration file uses the name of the project file with the extension changed to configuration. For example, the project AllTests.nunit would require a configuration file named AllTests.config, located in the same directory as AllTests.nunit. The same rule is followed when loading Visual Studio projects or solutions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top