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

后端 未结 4 827
温柔的废话
温柔的废话 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=<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%
    
    0 讨论(0)
  • 2021-01-31 03:22

    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

    0 讨论(0)
  • 2021-01-31 03:23

    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.

    0 讨论(0)
  • 2021-01-31 03:27

    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.

    0 讨论(0)
提交回复
热议问题