c# - 当我运行覆盖率测试时,我得到了测试方法代码的覆盖率,而不是我想要测试的方法的覆盖率

标签 c# code-coverage mstest opencover

我有一个项目,其中包含我想要测试的类和方法。我有另一个带有测试方法的项目,它将测试我的主项目的方法。

我使用 opencover 运行测试,并使用 reportgenerator 生成报告,使用 .bet 文件中的以下命令:

..\tools\OpenCover.Console.exe -register:user -target:"C:\myDllWithTests.dll" -output:"c:\coverage\opencovertests.xml"

.\ReportGenerator.exe "-reports:c:\coverage\opencovertests.xml" "-targetdir:c:\coverage\opencovertests.xml\reports"

我正在使用 MSTest 进行测试。

问题是,在 html 报告中,我看到覆盖的代码是测试方法,而不是我的测试主项目中的方法。

如何在结果中添加主要方法?

谢谢。

最佳答案

在 OpenCover 的 target 参数中,传递 MSTest 的路径(例如“C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe”)并指定您的测试targetargs 参数中的程序集(例如“C:\myDllWithTests.dll”)。

要从代码覆盖率统计中删除测试程序集,请在 filter argument 中指定它们.

下面是 OpenCover 命令,对我来说效果很好。这里被测试的代码放置在SampleApp.dll中,测试代码放置在SampleApp.Tests.dll中。

.\OpenCover.Console.exe -register:user -mergebyhash -target:"c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\MSTest.exe" -targetargs:"/testcontainer:\"d:\test\SampleApp\SampleApp.Tests\bin\Debug\SampleApp.Tests.dll\"" -output:UTResults.xml -filter:"+[SampleApp*]* -[SampleApp.Tests]*"

结果报告仅包含 SampleApp.dll 程序集的统计信息,不包括 SampleApp.Tests.dll:

enter image description here

检查这个answer了解更多详细信息。 还有一个great article作者:Allen Conway,介绍如何使用 OpenCover 和 ReportGenerator 进行 .Net 项目。

关于c# - 当我运行覆盖率测试时,我得到了测试方法代码的覆盖率,而不是我想要测试的方法的覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44225124/

相关文章:

c# - 将 ContextMenu 绑定(bind)到 DataGridCell 内容以复制内容

c# - Coverity 是否支持 xbuild (mono)?

c# - 您在测试中投入了多少断言?

xcode - 如何使用 llvm 累积跨应用程序执行的 xcode4 代码覆盖率

unit-testing - 在MSTest运行时动态创建单元测试方法

c# - 远程服务器返回 404

java - Sonar 代码覆盖率不会反射(reflect)在没有类文件的服务器上

python - 如何使用nosetests从python覆盖率报告中排除模拟包

c# - 与 Microsoft.VisualStudio.TestTools.UnitTesting 命名空间相关的错误

c# - Assert.AreEqual 失败但不应该