Coder Social home page Coder Social logo

example-csharp's Introduction

Codecov C# Example

https://codecov.io @codecov [email protected]

Installation

You need to add the OpenCover nuget package to your Visual Studio solution which is used to generate code coverage analysis:

PM> Install-Package OpenCover

Secondly, you need to either write a PowerShell script (if you intend to generate code coverage and upload the result interactively) or you need to add a few entries in you CI config file (if you intend to let your CI generate the coverage).

Generate the coverage file

Let's assume your C# solution has the following folder structure:

  • C:_build\MyProject\ --> this is your solution's root folder. For instance, this is where you have saved your visual studio solution (.sln) file.
  • C:_build\MyProject\packages\ --> this is where all nuget packages are downloaded
  • C:_build\MyProject\MyProject Unit Tests\ --> this is where your C# unit testing project is located (.csproj)

To generate code coverage analysis, execute the following comment from a command prompt in your solution's root:

.\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe -register:user -target:"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\MSTest.exe" -targetargs:"/noresults /noisolation /testcontainer:"".\MyProject Unit Tests\bin\Debug\MyProject.UnitTests.dll" -filter:"+[MyProject]*  -[MyProject]MyProject.Properties.*" -excludebyattribute:*.ExcludeFromCodeCoverage* -hideskipped:All -output:.\MyProject_coverage.xml

This command makes a few assumptions which you will most likely have to adjust to meet your needs:

  1. Use Visual Studio 2013
  2. Use OpenCover v4.6.519, which is the latest version as of this writing, to generate code coverage. Of course you will need to adjust the path to the OpenCover package when new versions are available.
  3. Unit tests are compiled in a DLL called .\MyProject Unit Tests\bin\Debug\MyProject.UnitTests.dll (replace 'Debug' with 'Release if you compile in 'Release' mode).
  4. Code coverage will include all classes, methods and properties under the [MyProject] namespace except [MyProject]MyProject.Properties. These properties, typically, include your project version number, compiler options, etc. and therefore should be excluded from the code coverage report.
  5. Any class decorated with the 'ExcludeFromCodeCoverage' attribute is automatically excluded from the coverage analysis.
  6. Code coverage analysis is written to a XML file called MyProject_coverage.xml

Now that you have generated your code coverage analysis, you must download Codecov's "upload script" and use it to upload your code coverage file to CodeCov.

##PowerShell script

The PowerShell script contains two lines: one to download Codecov's script and the second one to upload your coverage file to Codecov.io

(New-Object System.Net.WebClient).DownloadFile("https://codecov.io/bash", ".\CodecovUploader.sh")
.\CodecovUploader.sh -f "MyProject_coverage.xml -t <your upload token> -X gcov

Of course, you need to replace the <your upload token> with your private Codecov upload token.

TIP: for added convenience, you can paste the command from the 'Generate the Coverage file' at the top of this PowerShell script in order to create one convenient script that will both generate the coverage file and upload it to Codecov.io

Continuous Integration

If you use a CI, such as AppVeyor for example, you need to modify the 'after_test' section of your .yml to instruct your CI to generate the coverage file, download Codecov's uploader and finally to upload your coverage file to codecov.io. Here's what the 'after_test' section should look like:

after_test: 
    - .\packages\OpenCover.4.6.519\OpenCover.Console.exe -register:user -target:"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\MSTest.exe" -targetargs:"/noresults /noisolation /testcontainer:"".\MyProject Unit Tests\bin\Debug\MyProject.UnitTests.dll" -filter:"+[MyProject]*  -[MyProject]MyProject.Properties.*" -excludebyattribute:*.ExcludeFromCodeCoverage* -hideskipped:All -output:.\MyProject_coverage.xml
    - "SET PATH=C:\\Python34;C:\\Python34\\Scripts;%PATH%"
    - pip install codecov
    - codecov -f "MyProject_coverage.xml"

We recently incorporated codecov.io into DotNetAnalyzers/StyleCopAnalyzers. Based on this experience, I recommend the following changes to the documentation.

Combine test and code coverage stages

The steps to do this are elusive, but relatively straightforward when you see them.

  1. Add the -returntargetcode argument to OpenCover.Console
  2. Change the after_test section to test_script

๐Ÿ’ญ It might make sense to make a note that the code coverage gathering process may be run as a separate build step for users who are unable to or do not wish to run their complete automated testing through OpenCover. Users will want to integrate Codecov either in their test_script section or in their after_test section, but not both.

Include information for XUnit users

Users working with XUnit will need to modify their OpenCover call as follows:

  1. The -target argument becomes the following:
-target:"%xunit20%\xunit.console.x86.exe"
  1. The -targetargs argument becomes the following (you may want to replace our target assembly path with the one in your original sample; I wasn't familiar enough with the escape sequences for spaces to do it myself):
-targetargs:"C:\projects\stylecopanalyzers\StyleCop.Analyzers\StyleCop.Analyzers.Test\bin\Debug\StyleCop.Analyzers.Test.dll -noshadow -appveyor"

Important notes

  • BuildOptions should not include "portable" option on the dotnetcore test project's project.json file.

Sample project

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.