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

Deadly 提交于 2019-12-02 16:12:43
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

"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

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"

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"

Hope this helps.

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.

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

  • Generate an output report of unit test metrics using OpenCover
  • Generating a .htm report using ReportGenerator
  • Analyzing the output data to interpret unit test coverage metrics

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

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

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

  • OpenCover
  • NUnit.ConsoleRunner
  • ReportGenerator by 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.

@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%

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

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

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:

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!