Git Product home page Git Product logo

codecoverageexample's Introduction

CodeCoverageExample

Example of how to do code coverage on a .NET 5 project.
The resulting OpenCover code coverage calculation can be used in SonarQube.
ReportGenerator can optionally be used to create an Html report of code coverage (Cobertura output format).

About

This example is based on https://github.com/wulfland/code-coverage-sample and this article.
There is just one problem with the solution presented in the article - if you have more than one test project, test results are not merged.
Only the last project for which the test is executed is taken into consideration when calculating code coverage.
The example in this repository fixes that problem.

How to run the example

To build the solution run:
dotnet build -c Release

To calculate code coverage run:
dotnet test -c Release --logger trx --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=opencover%2cjson /p:CoverletOutput=PATH_TO_CURRENT_DIRECTORY\coverage\ /p:MergeWith=PATH_TO_CURRENT_DIRECTORY\coverage\coverage.json /m:1

Or simply execute RunBuildAndCodeCoverage.ps1 to do both.

How is the problem with the merging of code coverage results fixed

Compared with the original example/article, the following changes have been made:

  • CoverletOutputFormat was changed from opencover to opencover%2cjson
    • merging is supported for json format, but not for opencover format
    • this is why we create both, do merging using json, and in the end produce both json and opencover
    • usage of %2c separator is explained here
  • /p:MergeWith argument was added to merge code coverage results
  • /m:1 argument was added to force sequential execution of tests
    • this might not be needed, but is listed as needed here

Generating Html report from opencover.xml using ReportGenerator

Get ReportGenerator and in the folder where coverage.opencover.xml is located run:
.\reportgenerator "-reports:coverage.opencover.xml" "-targetdir:." "-reporttypes:Cobertura;HTMLInline;HTMLChart"

In the generated Html we can see that code coverage is 100% ๐Ÿ˜ƒ

Code Coverage Report

codecoverageexample's People

Contributors

kurtanr avatar

Stargazers

 avatar  avatar

Forkers

curlybytes

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.