13

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

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

AssemblyA和调用. AssemblyB_ ConfigurationManager如果我在 NUnit-Gui 中独立运行这些测试程序集,ConfigurationManager将正确解析本地配置文件。

但是,如果我加载testProject.nunit到 NUnit-Gui(其中包含对AssemblyA和的引用AssemblyB),无论当前正在执行哪个程序集,都会在其中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>
4

5 回答 5

5

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 时它会提示错误

-Null 引用异常.......对象瞬间未设置为......

解决方案 - 当您运行 Nunit GUI 时

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

这是解决您问题的简单方法

于 2012-03-14T06:50:18.990 回答
3

MarkLawrence 给出configSource元素解决方案正是我一直在寻找的,并且效果很好。然而,实现此解决方案的挑战是在从显式 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);
    }
}

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

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

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

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

于 2009-09-19T18:53:30.453 回答
2

NUnit 博客解释了为什么配置文件以它们的方式加载。基本上他们所说的是 NUnit 让框架处理配置文件而不做任何管理。

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

最后一种选择是使用configSource元素属性来使用程序集配置文件之一中的部分。

希望这可以帮助。

于 2009-09-13T01:55:11.433 回答
2

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


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

于 2010-03-23T05:33:36.963 回答
0

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

复制 /Y “$(ProjectDir)App.config” “$(TargetDir)$(TargetFileName).config”</p>

当您在测试中访问 ConfigurationManager 时,NUnit 会将您的 app.config 中的值传递给初始化方法中的 .Net。

于 2014-07-30T18:51:58.867 回答