如何指示 NUnit 从特定目录加载程序集的 dll.config 文件?

发布于 2024-08-04 16:54:47 字数 1082 浏览 3 评论 0原文

如果程序集包含 app.config 文件,只要它与通过 NUnit-Gui 执行的 NUnit 项目位于同一目录中,ConfigurationManager 就会加载它。为了说明这一点,请考虑以下文件夹结构。

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

AssemblyAAssemblyB 都执行调用 ConfigurationManager 的代码。如果我在 NUnit-Gui 中独立运行这些测试程序集,ConfigurationManager 将正确解析本地配置文件。

但是,如果我将 testProject.nunit 加载到 NUnit-Gui(其中包含对 AssemblyAAssemblyB 的引用),ConfigurationManager 在 TestFolder 中查找配置文件,无论当前正在执行哪个程序集。

有没有一种方法可以指示 NUnit 将应用程序配置重新加载到当前程序集目录中的配置?

以下是 testProject.nunit 的内容:

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

以往的大感动 2024-08-11 16:54:47

Nunit 无法在我们的项目中找到 App.config 文件的路径。因此,我们需要手动告诉 Nunit App.config 文件放置在我们项目中的位置(显然是在根文件夹中)。

就我而言,项目结构如下,

     +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

ProjectWebApp 使用包含业务逻辑的projectBusinesslogic 的引用。
+ProjectTestName 使用projectBusinesslogic 的引用来对业务逻辑进行测试。
问题从这里开始,Nunit 测试项目需要自己的 app.config 文件。它不会像projectBusinesslogic那样使用web.config文件,所以当你运行Nunit时它会提示错误

-空引用异常......对象即时未设置为......

解决方案-
当您运行 Nunit GUI

  1. 项目 -> 编辑一个新的弹出窗口时,将打开
  2. 属性 ->常规->配置文件名->添加您的 app.config 文件名
  3. File-> 保存并关闭弹出窗口
  4. ON Nunit Gui-File->重新加载项目

,这就是您问题的简单解决方案

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

蓝眸 2024-08-11 16:54:47

configSource 元素 MarkLawrence 给出的解决方案正是我一直在寻找的,并且效果很好。然而,实现此解决方案的挑战是在从显式 NUnit 项目运行测试时加载程序集配置(如 我的案例),并且在单独运行程序集的单元测试时(没有明确的项目)。为了实现这一点,需要对我的文件布局进行以下修改。

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

创建 configfragment 文件以包含曾经位于相应 config 文件中的程序集配置。然后,config 文件被修改为仅包含 configSource 元素以及对应 configfragment 文件的相对路径。请注意,此方法唯一不起作用的情况是当 assemblyA.dll 和 assemblyB.dll 都需要相同的配置节时,因为在创建 <代码>testproject.config

在尝试了这种方法之后,我决定依靠在运行时生成程序集配置,并将静态配置文件全部删除。下面是一些代码,演示如何生成配置,并使其可访问,无论如何加载被测程序集。

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);
    }
}

通过使用不接受路径中,我们从主机应用程序的工作目录加载配置,该目录根据您运行 NUnit 测试的方式而变化。此外,try/finally 块保证您的程序集配置不会干扰其他测试,这些测试可能需要也可能不需要此类配置。

最后,在单元测试中使用:

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

我希望这可以帮助其他可能遇到类似问题的人!

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!

海的爱人是光 2024-08-11 16:54:47

NUnit 博客解释了为什么配置文件会这样加载。基本上他们所说的是 NUnit 让框架处理配置文件,但不进行任何管理。

您还可以使用将在您的案例中加载的 testProject.config 文件来引用每个程序集的配置文件。使用 appSettings 文件属性添加密钥。

最后一种替代方法是使用 configSource 元素属性,用于使用程序集配置文件之一中的部分。

希望这有帮助。

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.

白鸥掠海 2024-08-11 16:54:47

在 .nunit 文件的配置级别中使用 configfile 属性:


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

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


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

听风念你 2024-08-11 16:54:47

实际上,如果您使用 NUnit 和 Visual Studio 中的运行程序,您可以将您的值粘贴到测试项目的 App.config 中。然后将此行添加到您的构建后事件中:

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

当您在测试中访问 ConfigurationManager 时,NUnit 将传递以下值在初始化方法中将您的 app.config 更改为 .Net。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文