代码之家  ›  专栏  ›  技术社区  ›  Steve Guidi

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

  •  13
  • Steve Guidi  · 技术社区  · 15 年前

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

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

    二者都 AssemblyA AssemblyB 配置管理器 . 如果我在NUnit Gui中独立运行这些测试程序集, 配置管理器

    但是,如果我加载 testProject.nunit 议会议员 装配B ), 配置管理器 在中查找配置文件 TestFolder

    应用程序的当前配置中是否有一个NUN要重新加载?

    以下是 testProject.nunit :

    <NUnitProject>
      <Settings activeconfig="Debug" />
      <Config name="Debug" binpathtype="Auto">
        <assembly path="AssemblyAFolder\assemblyA.dll" />
        <assembly path="AssemblyBFolder\assemblyB.dll" />
      </Config>
    </NUnitProject>
    
    5 回复  |  直到 15 年前
        1
  •  5
  •   Anton Sizikov    12 年前

    在我的例子中,项目结构如下

         +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
    

    +ProjectTestName使用projectBusinesslogic的引用对业务逻辑执行测试。 问题从这里开始,Nunit测试项目需要自己的app.config文件。它不会像projectBusinesslogic那样使用web.config文件,因此当您运行Nunit时,它会提示一个错误

    -空引用异常…….对象瞬间未设置为。。。。。。。。。。。

    解决方案-

    1. 项目->编辑一个新的弹出窗口将打开
    2. 房地产->一般->配置文件名->添加您的 文件名
    3. 文件->保存并关闭弹出窗口
    4. 在Nunit Gui文件上->重新加载项目

        2
  •  3
  •   Community CDub    8 年前

    这个 configSource solution given by MarkLawrence 是我一直在寻找的,而且效果很好。然而,实现此解决方案的挑战是在从显式NUnit项目(如中所示)运行测试时加载程序集配置 my case ),并且在隔离运行程序集的单元测试时(无显式项目)。为此,需要对我的文件布局进行以下修改。

    + 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 文件夹。之后 配置 文件被修改为仅包含 元素的相对路径 配置片段 文件请注意,这种方法唯一不起作用的时间是 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
        });
    }
    

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

        3
  •  2
  •   Jeroen    10 年前

    testProject.config 文件,以引用每个程序集的配置文件。使用appSettings文件属性添加密钥。

    最后一种选择是使用 configSource element attribute 使用其中一个程序集配置文件中的节。

    希望这有帮助。

        4
  •  2
  •   Egil    10 年前

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

    
    <Config name="Debug" configfile="myconfigfilenamegoeshere.config />
    
    
        5
  •  0
  •   Steven Hoff    10 年前

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

    复制/Y$(ProjectDir)App.config$(TargetDir)$(TargetFileName.config

    当您在测试中访问ConfigurationManager时,NUnit将通过initialize方法将app.config中的值传递到.Net。