MSTest 失败:无法在应用程序的 .config 文件中找到配置部分 'log4net'

MSTest fails: Failed to find configuration section 'log4net' in the application's .config file

这是我的设置:

/
solution.sln
Core/
    Core.csproj
web/
    web.csproj
    Web.config
Test/
    Test.csproj
    App.config
build/
    _PublishedWebsites/web/etc...
    test.dll
    test.dll.config
    Core.dll
    log4net.dll

我在 Test 项目中编写单元测试来测试 Core 项目的功能。所有项目都使用 log4net,所有项目都使用 App.configWeb.config 文件使用

配置
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

在他们的 AssemblyInfo.cs.

项目是使用 NAnt 构建到 build 目录中的。

当我 运行 来自 Visual Studio (2010 Professional) 的单元测试时,一切正常。

但是当我尝试直接执行 MSTest 时

MSTest.exe /resultsfile:TestResults.trx /noisolation /testcontainer:build\test.dll

所有测试用例均失败并出现以下错误:

log4net:ERROR Failed to find configuration section 'log4net' in the application's .config file

这让我感到困惑,因为 test.dll.config 文件肯定包含 log4net 配置部分。

MSTest 创建的文件夹还包含配置文件:

/flopes_MyMachine 2015-10-08 08_48_59/Out/
    Core.dll
    test.dll
    test.dll.config
    log4net.dll

在我看来,也许 MSTest 使用了错误的配置文件或没有找到 test.dll 的配置文件?这有意义吗?有没有办法告诉 MSTest 使用哪个配置文件?

我做了一个小的测试输出,看看加载了什么配置文件,它增加了更多的神秘感,因为显然加载的配置文件是:

D:\Programme\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe.Config

这怎么可能?为什么 MSTest 不加载 就在 test.dll 旁边的配置文件?这有什么意义?

我会看看这个问题和答案:MSTest.exe not finding app.config

这表明您应该创建一个测试 运行 配置,其中包括 .config 作为部署项。

还要检查正在使用的配置是否正确;查找活动配置文件的方法:

// Get the user-config:
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath

// Simple approach
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile? Is it the correct .config

与@florianpeschka 核对了几件事情让我们相信问题出在 noisolation-flag。这有点奇怪,因为 noisolation 基本上控制为测试实例创建单独的沙箱,但可能需要创建单独的进程才能使用 "dynamic" 和 ms 的非硬编码配置-测试。

简而言之,不要使用 noisolation-flag,一切正常。