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

后端 未结 4 826
温柔的废话
温柔的废话 2021-01-31 02:52

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

4条回答
  •  借酒劲吻你
    2021-01-31 03:06

    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=
    
    REM ** SET DLLToTestRelativePath=Calculator\bin\Debug\MyCalc.dll
    SET DLLToTestRelativePath=
    
    REM ** Filters Wiki https://github.com/opencover/opencover/wiki/Usage
    REM ** SET Filters=+[Calculator]* -[Calculator]CalculatorTests.*
    SET 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%
    

提交回复
热议问题