Search code examples
.netunit-testingnunitapp-configconfigurationmanager

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


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>

Solution

  • The NUnit blog explained of why the config files load the way they do. Basically what they said was that NUnit lets the framework handle the config files and doesn't do any of the management.

    You can also use the testProject.config file that would be loaded in your case, to reference the config files for each of the assemblies. Using the appSettings file attribute to add keys.

    One final alternative is to use the configSource element attribute to use the section in one of the assemblies config files.

    Hope this helps.