【发布时间】:2015-12-30 08:13:51
【问题描述】:
这是我的设置:
/
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.config 或 Web.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,它会改变吗? (即工作目录不同)
-
@flindeberg Nop,加载相同的配置文件,无论工作目录是什么。
-
您是否可以创建一个我可以查看的最小工作示例?我无法重新创建您的方案 =/(鉴于 .config 是作为部署项的部署的一部分)
-
@flindeberg 好吧......我也不能。我现在尝试创建一个 MWE 两个小时,但是当我从头开始时它以某种方式工作。这仍然是信息;这意味着除了我在此处列出的内容之外的其他东西肯定会导致问题......
-
奇怪,
noisolation基本上控制为测试实例创建单独的沙箱,但可能需要创建单独的进程才能为 ms-test 使用“动态”和非硬编码配置.
标签: c# unit-testing log4net mstest