How do you instruct NUnit to load an assembly's dll.config file from a specific directory?

Steve Guidi picture Steve Guidi · Sep 11, 2009 · Viewed 15.2k times · Source

If an assembly contains an app.config file, ConfigurationManager will load it as long as it is in the same directory as the NUnit project that is executing through NUnit-Gui. To illustrate consider the following folder structure.

+ TestFolder
    testProject.nunit
  + AssemblyAFolder
      assemblyA.dll
      assemblyA.dll.config
  + AssemblyBFolder
      assemblyB.dll
      assemblyB.dll.config

Both AssemblyA and AssemblyB exercise code that calls into ConfigurationManager. If I run these test assemblies independently in NUnit-Gui, ConfigurationManager will correctly resolve the local configuration files.

However, if I load testProject.nunit into NUnit-Gui (which contains references to both AssemblyA and AssemblyB), ConfigurationManager looks for the configuration file in TestFolder regardless of which assembly is currently executing.

Is there a way to direct NUnit to reload the application configuration to the one present in the current assembly's directory?

Here are the contents of testProject.nunit:

<NUnitProject>
  <Settings activeconfig="Debug" />
  <Config name="Debug" binpathtype="Auto">
    <assembly path="AssemblyAFolder\assemblyA.dll" />
    <assembly path="AssemblyBFolder\assemblyB.dll" />
  </Config>
</NUnitProject>

Answer

Mangesh Gulhane picture Mangesh Gulhane · Mar 14, 2012

Nunit is unable to locate the path of App.config File in our project. So we need to manually tell the Nunit where the App.config File is placed in our project (obviously on the root folder).

In my case , the project structure is as following

     +ProjectWEBApp//web pages
       +Modules
         +aspx pages
       +web.Config

    +projectBusinesslogic //business logic .cs files
       +Modules
         +.cs

      +ProjectTestName// a seperate Nunit test cases project
        +Modules
      +App.Config

the ProjectWebApp uses the references of projectBusinesslogic which contains the business logic. the +ProjectTestName uses the reference of projectBusinesslogic to perform test on the business logic. The problems start here, Nunit test project needs its own app.config file. it won't use web.config file as in case of projectBusinesslogic,so when you run Nunit it will prompt an error

-Null Reference exception.......object instant not set to ...........

Solution- When you run the Nunit GUI

  1. Project->Edit a new pop up will open
  2. Properties -> General->Configuration File Name-> add your app.config File Name
  3. File->save and close the pop-up window
  4. ON Nunit Gui-File-> Reload Project

and that is the simple solution for your problem