.net 如何使用 OpenCover 和 ReportGenerator 查看单元测试覆盖率结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16591486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:15:27  来源:igfitidea点击:

How do I use OpenCover and ReportGenerator to view Unit Test Coverage Results?

.netunit-testingnuget-packageopencoverreportgenerator

提问by atconway

I'm a noob to using both OpenCover and ReportGenerator and I'm struggling a bit in understanding how to get them working. I'm using VS.NET 2012 'Professional' which means I don't have access to the built in unit test coverage tooling. I also have ReSharper installed, but don't want to pay for another utility in 'dotCover'

我是使用 OpenCover 和 ReportGenerator 的菜鸟,我在理解如何让它们工作方面有点挣扎。我正在使用 VS.NET 2012 'Professional',这意味着我无法访问内置的单元测试覆盖率工具。我也安装了 ReSharper,但不想为“dotCover”中的另一个实用程序付费

It looks like OpenCover and ReportGenerator will do what I need and I see the documentation that was downloaded alongside, but am missing some understanding. 1st off, when I download the nuget packges for both, what should my target project be? I have a multi layer app, so I'm assuming my unit test project correct, or does it even matter? I see in the documentation, I'm just pointing at the /bin (I think) of a solution using command line commands, so maybe I didn't even need to add these downloads to any particular project (could have been a test harness). Can someone tell me if I have this correct?

看起来 OpenCover 和 ReportGenerator 会做我需要的,我看到了随附下载的文档,但我缺少一些理解。首先,当我为两者下载 nuget 包时,我的目标项目应该是什么?我有一个多层应用程序,所以我假设我的单元测试项目是正确的,或者它甚至重要吗?我在文档中看到,我只是指向使用命令行命令的解决方案的 /bin(我认为),所以也许我什至不需要将这些下载添加到任何特定项目(可能是一个测试工具)。有人可以告诉我这是否正确?

Once I have them installed, I'm trying to get unit test coverage metrics, and the docs that came with the package are not as clear as I hoped. Are there any good blog posts or links that walk through using these tool together to get the metrics?

一旦我安装了它们,我就会尝试获取单元测试覆盖率指标,但软件包附带的文档并不像我希望的那样清晰。是否有任何好的博客文章或链接可以一起使用这些工具来获取指标?

回答by SonalKhodiyar

you do not need to add these to particular project

您不需要将这些添加到特定项目

I use report generator and open cover to generate test coverage results too. This is the script I use to generate the codecoverage using opencover

我也使用报告生成器和开放式封面来生成测试覆盖率结果。这是我用来使用 opencover 生成代码覆盖率的脚本

"C:\Program Files (x86)\OpenCover\OpenCover.Console.exe" -register:user -target:"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe" -targetargs:"/noisolation /testcontainer:\"C:\\bin\Debug\.dll\" /resultsfile:C:\Reports\MSTest\.trx" -filter:"+[]" -mergebyhash -output:C:\Reports\MSTest\projectCoverageReport.xml

"C:\Program Files (x86)\OpenCover\OpenCover.Console.exe" -register:user -target:"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe" -targetargs :"/noisolation /testcontainer:\"C:\\bin\Debug\.dll\" /resultsfile:C:\Reports\MSTest\.trx" -filter:"+[ ]" -mergebyhash -output:C:\报告\MSTest\projectCoverageReport.xml

Note that if your argument needs to escape quotes i.e. to pass arguments with spaces to that target process then you can use ", e.g.: -targetargs:"c:\program files"

请注意,如果您的参数需要转义引号,即将带空格的参数传递给该目标进程,那么您可以使用",例如:-targetargs:"c:\program files"

This is the script I use to run report generator.

这是我用来运行报告生成器的脚本。

C:\ReportGenerator\bin\ReportGenerator.exe -reports:"C:\Reports\MSTest\projectCoverageReport.xml" -targetdir:"C:\Reports\CodeCoverage"

C:\ReportGenerator\bin\ReportGenerator.exe -reports:"C:\Reports\MSTest\projectCoverageReport.xml" -targetdir:"C:\Reports\CodeCoverage"

Hope this helps.

希望这可以帮助。

回答by atconway

After several years of using these open source tools, I finally created a comprehensive post on how to use OpenCover and ReportCover to generate unit test coverage metrics.

在使用这些开源工具几年后,我终于写了一篇关于如何使用 OpenCover 和 ReportCover 生成单元测试覆盖率指标的综合文章。

The post describes how to create the .batfile and the commands needed to do the following:

该帖子描述了如何创建.bat文件以及执行以下操作所需的命令:

  • Generate an output report of unit test metrics using OpenCover
  • Generating a .htmreport using ReportGenerator
  • Analyzing the output data to interpret unit test coverage metrics
  • 使用 OpenCover 生成单元测试指标的输出报告
  • .htm使用 ReportGenerator生成报告
  • 分析输出数据以解释单元测试覆盖率指标

Using OpenCover and ReportGenerator to get Unit Testing Code Coverage Metrics in .NET

使用 OpenCover 和 ReportGenerator 获取 .NET 中的单元测试代码覆盖率指标

回答by Rob.Ak

Thanks @atconway for your tutorial. I've updated your .bat script a little, to ease future upgrades, and project changes.

感谢@atconway 的教程。我已经稍微更新了您的 .bat 脚本,以简化未来的升级和项目更改。

Summarizing, to use OpenCover with NUnit you have to add to your project these nugets:

总而言之,要将 OpenCover 与 NUnit 一起使用,您必须将这些 nuget 添加到您的项目中:

  • OpenCover
  • NUnit.ConsoleRunner
  • ReportGenerator by Daniel Palme
  • 开封
  • NUnit.ConsoleRunner
  • 报告生成器 Daniel Palme

and here is updated .bat file. To run it just edit "settings" and save script as .bat file in root of your project.

这里是更新的 .bat 文件。要运行它,只需编辑“设置”并将脚本保存为项目根目录中的 .bat 文件。

@echo off
REM ** Be sure to install these nugets:
REM ** NUnit.ConsoleRunner
REM ** OpenCover
REM ** ReportGenerator
REM **
REM ** All paths should be entered without quotes

REM ** SET TestResultsFileProjectName=CalculatorResults
SET TestResultsFileProjectName=<ANY_NAME>

REM ** SET DLLToTestRelativePath=Calculator\bin\Debug\MyCalc.dll
SET DLLToTestRelativePath=<VALID_PATH>

REM ** Filters Wiki https://github.com/opencover/opencover/wiki/Usage
REM ** SET Filters=+[Calculator]* -[Calculator]CalculatorTests.*
SET Filters=<VALID_FILTERS>

SET OpenCoverFolderName=OpenCover.4.6.519
SET NUnitConsoleRunnerFolderName=NUnit.ConsoleRunner.3.6.1
SET ReportGeneratorFolderName=ReportGenerator.2.5.6

REM *****************************************************************

REM Create a 'GeneratedReports' folder if it does not exist
if not exist "%~dp0GeneratedReports" mkdir "%~dp0GeneratedReports"

REM Remove any previous test execution files to prevent issues overwriting
IF EXIST "%~dp0%TestResultsFileProjectName%.trx" del "%~dp0%TestResultsFileProjectName%.trx%"

REM Remove any previously created test output directories
CD %~dp0
FOR /D /R %%X IN (%USERNAME%*) DO RD /S /Q "%%X"

REM Run the tests against the targeted output
call :RunOpenCoverUnitTestMetrics

REM Generate the report output based on the test results
if %errorlevel% equ 0 (
 call :RunReportGeneratorOutput
)

REM Launch the report
if %errorlevel% equ 0 (
 call :RunLaunchReport
)
exit /b %errorlevel%

:RunOpenCoverUnitTestMetrics
"%~dp0packages\%OpenCoverFolderName%\tools\OpenCover.Console.exe" ^
-register:user ^
-target:"%~dp0packages\%NUnitConsoleRunnerFolderName%\tools\nunit3-console.exe" ^
-targetargs:"--noheader \"%~dp0%DLLToTestRelativePath%\"" ^
-filter:"%Filters%" ^
-mergebyhash ^
-skipautoprops ^
-excludebyattribute:"System.CodeDom.Compiler.GeneratedCodeAttribute" ^
-output:"%~dp0GeneratedReports\%TestResultsFileProjectName%.xml"
exit /b %errorlevel%

:RunReportGeneratorOutput
"%~dp0packages\%ReportGeneratorFolderName%\tools\ReportGenerator.exe" ^
-reports:"%~dp0GeneratedReports\%TestResultsFileProjectName%.xml" ^
-targetdir:"%~dp0GeneratedReports\ReportGenerator Output"
exit /b %errorlevel%

:RunLaunchReport
start "report" "%~dp0GeneratedReports\ReportGenerator Output\index.htm"
exit /b %errorlevel%

回答by Roman Pokrovskij

It is a hell to configure opencover for multiple test containers.

为多个测试容器配置 opencover 是一个地狱。

Use this my smart Powershell script, it can give you some ideas.

使用这个我的智能 Powershell 脚本,它可以给你一些想法。

https://github.com/rpokrovskij/opencover4vs.ps1/blob/master/opencover4vs.ps1

https://github.com/rpokrovskij/opencover4vs.ps1/blob/master/opencover4vs.ps1

you need to configure two major things : how to find your test dlls and which namespaces to include to the output. I do it this way:

您需要配置两个主要内容:如何找到您的测试 dll 以及要包含到输出中的命名空间。我这样做:

$TestDllsPatterns = @(,'*\bin\Debug\Vse.*.Test.dll')  
$TestableCodeNamespacePatterns = @(,'*') 

Note, my tests starts from prefix Vse. You need the Debug\Prefixkey to filter out core nUnit tests from folders like Debug\netcore1.1\Vse.

注意,我的测试从前缀 Vse 开始。您需要使用Debug\Prefix密钥从Debug\netcore1.1\Vse.