How to upload xUnit coverage reports to Codecov?

368 Views Asked by At

I have a .Net 5 solution with multiple xUnit test projects which is a public repository hosted on Github. I would like to generate code coverage reports and display them on Codecov.

First I run

dotnet add package coverlet.msbuild

for each test project. I know that I can navigate to the .sln directory and generate a new report via

dotnet test /p:CollectCoverage=true

I authorized Codecov, so it knows about this project. I think only the main and dev branches are relevant so I started with this workflow

name: Generate coverage report on push

on:
  push:
    branches:
      - 'main'
      - 'dev'

jobs:
  generate-coverage-report-on-push:
    runs-on: ubuntu-latest

    defaults:
      run:
        working-directory: ./DirectoryWithSlnFile

    steps:
      - uses: actions/checkout@v2

      - name: Setup .NET
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: 5.0.x

      - name: Restore dependencies
        run: dotnet restore

      - name: Build project
        run: dotnet build --no-restore

      - name: Generate coverage report
        run: dotnet test --no-build --verbosity normal /p:CollectCoverage=true

What needs to be done now to upload the report from all test projects to Codecov? E.g.

      - name: Upload coverage report
        run: upload to Codecov with CODECOV_TOKEN if pushed on main or dev
1

There are 1 best solutions below

0
On

I just stumbled upon this when having the same question. This is how I made it work.

First, the step you already had but just for completeness. Add coverlet to the test project(s):

dotnet add package coverlet.msbuild

Then, in the GitHub Actions workflow file, add the following steps:

    - name: Test
      run: dotnet test --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
    
    - name: Codecov
      uses: codecov/codecov-action@v2
      with:
        file: coverage.opencover.xml
        directory: FUI.Tests