代码从配置文件中读取数据时,OpenCover.Console测试失败

时间:2015-09-02 08:13:31

标签: c# unit-testing opencover

我有这个奇怪的问题。当我从Visual Studio使用OpenCover时,所有测试都通过,一切似乎都很好。

来自Visual Studio的图片:http://i.stack.imgur.com/awmWy.png

但是当我想使用OpenCover.Console.exe自动化测试时,某些测试失败了。我注意到2个失败的测试从配置文件中读取。

测试或测试的代码使用 ConfigurationManager.AppSettings [" someConfigValue"] 从配置文件中读取。 我在主项目和测试项目 appsettings 中有相同的值。

从控制台映像执行时的两个失败测试:http://i.stack.imgur.com/a77Tt.png

另外,我提供了用于OpenCover.Console.exe的命令。

  

" OpenCover.Console.exe"   -寄存器   -target:" C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ Common7 \ IDE \ MSTest.exe"   -targetargs:" / noisolation /testcontainer:\"E:\TEAM\TestProject.Tests\bin\Debug\TestProject.Tests.dll" /resultsfile:TestResults.trx"   -mergebyhash   -output:CodeCoverage.xml

更新[ 2015年9月4日]

由于我没有找到任何特定的解决方案,我想出了一个解决方法。 实际上,我认为配置文件及其所有数据不属于单元测试,而是属于集成测试。

所以在我的代码中我不再使用ConfigurationManager.AppSettings [" someConfigValue"]了。我所做的是创建一个ConfigurationService,在IConfigurationService中,我有从配置文件中获取值所需的所有方法。

将此注入解决方案的BL中,我使用Mocks进行单元测试。

0 个答案:

没有答案