Codecov: C# example repository
Switch branches/tags
Nothing to show
Clone or download
Latest commit 1842735 Apr 26, 2018
Permalink
Failed to load latest commit information.
MyUnitTests Major overhaul Jun 12, 2017
UnitTestTargetProject Major overhaul Jun 12, 2017
.gitattributes Major overhaul Jun 12, 2017
.gitignore Major overhaul Jun 12, 2017
CodecovProject.sln Major overhaul Jun 12, 2017
LICENSE Major overhaul Jun 12, 2017
README.md Update README.md Apr 26, 2018
appveyor.yml Major overhaul Jun 12, 2017

README.md

Codecov C# Example

https://codecov.io @codecov hello@codecov.io

AppVeyor codecov

Solution

Start by restoring the nuget packages and building the solution.

Generate the Coverage File

Coverage is generated using OpenCover. You can obtain it from NuGet or Chocolatey. If we run the following command in PowerShell to install OpenCover via Chocolatey,

choco install opencover.portable

the OpenCover commandline will become available.

Generation of coverage report is slighly different depending on the .NET platform of your test projects.

.NET Framework project

xUnit

First install the xUnit console runner via Nuget or Chocolatey. If we run the following in PowerShell to install xUnit via Chocolatey

choco install xunit

and execute the following in your solution's root,

OpenCover.Console.exe -register:user -target:"xunit.console.x86.exe" -targetargs:".\MyUnitTests\bin\Debug\MyUnitTests.dll -noshadow" -filter:"+[UnitTestTargetProject*]* -[MyUnitTests*]*" -output:".\MyProject_coverage.xml"

Then a coverage report will be generated.

MSTest

Execute the following in your solution's root,

OpenCover.Console.exe -register:user -target:"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\MSTest.exe" -targetargs:"/testcontainer:"".\MyUnitTests\bin\Debug\MyUnitTests.dll" -filter:"+[UnitTestTargetProject*]* -[MyUnitTests*]*" -output:".\MyProject_coverage.xml"

where you may need to change the -target flag to point to the correct version of MSTest.

.NET Core project

If you don't yet have .NET Core SDK installed, install it

choco install dotnetcore-sdk

In case of .NET Core projects, there is no difference between MSTest and xUnit for coverage report generation.

Make sure all covered projects generate full pdb file (not only test projects), either through <DebugType>full</DebugType> in the .csproj file or by using a Visual Studio: Project Properties > Build > Advanced > Debugging information. By default, projects created by dotnet or by Visual Studio use a portable format for pdb files. Support for portable pdb format hasn't been released in OpenCover yet. If you fail to set full pdb, the OpenCover will print out a message notifying you that it has no results along with common causes.

The .NET Core test assembly can't be run by a xunit.console.x86.exe, because that tool works only with .NET Framework assemblies. The tests are run by dotnet test (possibly dotnet xunit if you add dotnet-xunit CLI tool to your project).

Execute the following command in your solution's root:

OpenCover.Console.exe -register:user -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:test -filter:"+[UnitTestTargetProject*]* -[MyUnitTests*]*" -output:".\MyProject_coverage.xml" -oldstyle

where -oldstyle switch is necessary, because .NET Core uses System.Private.CoreLib instead of mscorlib and thus OpenCover can't use mscorlib for code instrumentation. You may also need to change the location of dotnet.exe to depending on the installed location.

Uploading Report

Many options exit for uploading reports to Codecov. Three commonly used uploaders for .NET are

  1. Codecov-exe (C# source code)
  2. Bash
  3. Python

For OS X and Linux builds, the recommended uploader is bash. For windows builds, all three uploaders work, but Codecov-exe does not require any dependencies. For example, the bash uploader and python uploader would require bash or python to be installed. This may or may not be an option.

Codecov-exe

First install Codecov-exe via Nuget or Chocolatey. If we run the following in PowerShell to install it via Chocolatey

choco install codecov

and then run the following in PowerShell

.\codecov -f "MyProject_coverage.xml" -t <your upload token>

the report will be uploaded.

Bash

In bash run the following to upload the report

curl -s https://codecov.io/bash > codecov
chmod +x codecov
./codecov -f "MyProject_coverage.xml" -t <your upload token>

Python

First installed python (if you don't have it already). A simple way to install python is Chocolatey

choco install python

Next run the following in PowerShell

pip install codecov
.\codecov -f "MyProject_coverage.xml" -t <your upload token>

Continous Integration

The previous examples assumed local development. More commonly, you'll use a CI service like AppVeyor or TeamCity. For TeamCity builds please see the documentation. For AppVeyor builds using xUnit, your yaml file would look something like

Codecov-exe using Chocolatey

image: Visual Studio 2015

before_build:
- nuget restore
- choco install opencover.portable
- choco install codecov

build:
  project: CodecovProject.sln
  verbosity: minimal

test_script:
- OpenCover.Console.exe -register:user -target:"%xunit20%\xunit.console.x86.exe" -targetargs:".\MyUnitTests\bin\Debug\MyUnitTests.dll -noshadow" -filter:"+[UnitTestTargetProject*]* -[MyUnitTests*]*" -output:".\MyProject_coverage.xml"
- codecov -f "MyProject_coverage.xml"

Codecov-exe using NuGet

Using this method you can cache your packages.config file.

image: Visual Studio 2015

before_build:
- nuget restore

build:
  project: CodecovProject.sln
  verbosity: minimal

test_script:
- .\packages\<ADD PATH>\OpenCover.Console.exe -register:user -target:"%xunit20%\xunit.console.x86.exe" -targetargs:".\MyUnitTests\bin\Debug\MyUnitTests.dll -noshadow" -filter:"+[UnitTestTargetProject*]* -[MyUnitTests*]*" -output:".\MyProject_coverage.xml"
- .\packages\<ADD PATH>\codecov.exe -f "MyProject_coverage.xml"

Python

image: Visual Studio 2015

before_build:
- nuget restore
- choco install opencover.portable

build:
  project: CodecovProject.sln
  verbosity: minimal

test_script:
- OpenCover.Console.exe -register:user -target:"%xunit20%\xunit.console.x86.exe" -targetargs:".\MyUnitTests\bin\Debug\MyUnitTests.dll -noshadow" -filter:"+[UnitTestTargetProject*]* -[MyUnitTests*]*" -output:".\MyProject_coverage.xml"
- "SET PATH=C:\\Python34;C:\\Python34\\Scripts;%PATH%"
- pip install codecov
- codecov -f "MyProject_coverage.xml"

Cake.Codecov

If you use Cake (C# Make) for your build automation, there is a Cake.Codecov addin available. Cake also has built in support for OpenCover. It makes using OpenCover and Codecov-exe really easy!

Sample Project

An example C# project using AppVeyor, xUnit, OpenCover, and Codecov-exe is DotNetAnalyzers/StyleCopAnalyzers.

We are happy to help if you have any questions. Please contact email our Support at support@codecov.io