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

做~自己de王妃 提交于 2020-01-10 18:01:07

问题


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>

回答1:


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.




回答2:


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




回答3:


The configSource element solution given by MarkLawrence is what I was looking for, and works nicely. The challenge in implementing this solution however, is to make the assembly configuration load when running tests from both an explicit NUnit project (as in my case), AND when running the unit tests for an assembly in isolation (no explicit project). To accomplish this, the following modifications to my file layout were required.

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

The configfragment files are created to contain the assembly configuration that was once in the corresponding config files. Afterwards, the config files are modified to contain only a configSource element with a relative path to the corresponding configfragment file. Note that the only time that this approach doesn't work is when assemblyA.dll and assemblyB.dll both require the same configuration section, as a conflict will arise when creating testproject.config.

After experimenting with this approach I decided to rely on generating the assembly configuration at runtime, and remove the static configuration files all together. Here is some code that demonstrates how to generate the configuration, and make it accessible regardless of how the assembly-under-test is loaded.

void WithConfigurationFile(Action method)
{
    // Create the assembly configuration.
    string settingsSection = "myConfigSectionName";
    Configuration config =
        ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    config.Sections.Add(
        settingsSection,
        new ConfigSectionType(/*config element values*/);
    config.Save();

    try
    {
        // Invoke the method with the new configuration.
        ConfigurationManager.RefreshSection(settingsSection);
        method();
    }
    finally
    {
        // Revert the assembly configuration.
        File.Delete(config.FilePath);
        ConfigurationManager.RefreshSection(settingsSection);
    }
}

By using the ConfigurationManager.OpenExeConfiguration() overload that does not accept a path, we load the configuration from the host application's working directory, which changes depending on how you run your NUnit tests. Also, the try/finally block guarantees that your assembly configuration will not interfere with other tests, which may or may not require such configuration.

Finally, to use within a unit test:

[Test]
void VerifyFunctionality
{
    WithConfigurationFile(delegate
    {
        // implement unit test and assertions here
    });
}

I hope this helps others who may have encountered similar issues!




回答4:


Use the configfile attribute in the Config level in your .nunit file:


<Config name="Debug" configfile="myconfigfilenamegoeshere.config />




回答5:


Actually, if you are using NUnit and a runner within Visual Studio, you can stick your value in an App.config in your test project. Then add this line to your Post-build event:

copy /Y “$(ProjectDir)App.config” “$(TargetDir)$(TargetFileName).config”

When you access the ConfigurationManager in your tests, NUnit will pass the values in your app.config to .Net in the initialize method.



来源:https://stackoverflow.com/questions/1412235/how-do-you-instruct-nunit-to-load-an-assemblys-dll-config-file-from-a-specific

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!