https://codecov.io | @codecov | hello@codecov.io |
---|
Start by restoring the nuget packages and building the solution.
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.
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.
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.
Many options exit for uploading reports to Codecov. Three commonly used uploaders for .NET are
- Codecov-exe (C# source code)
- Bash
- 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.
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.
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>
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>
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
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"
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"
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"
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!
An example C# project using AppVeyor, xUnit, OpenCover, and Codecov-exe is DotNetAnalyzers/StyleCopAnalyzers.