Partial code coverage for C++ with codecov and GitHub actions

A very short one on generating code coverage for C++, with lcov and codecov, inside GitHub actions.

Although I have tests covering all scenarios, I saw a boolean condition being reported as partially covered by Codecov.

bool is_closed;

if (is_closed) {
  //...
}

  coverage:
    name: Coverage
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Create Build Environment
        run: cmake -E make_directory ${{github.workspace}}/build

      - name: Configure CMake
        working-directory: ${{github.workspace}}/build
        run: cmake ${{ github.workspace }} -DCMAKE_BUILD_TYPE=Debug -DCPP_CHANNEL_BUILD_TESTS=ON -DCPP_CHANNEL_COVERAGE=ON

      - name: Build
        working-directory: ${{github.workspace}}/build
        run: cmake --build . --config Debug --target channel_tests -j

      - name: Test
        working-directory: ${{github.workspace}}/build
        run: ctest -C Debug --verbose -L channel_tests -j

      - name: Upload coverage reports to Codecov
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}

 

You can help Codecov by generating coverage.info with lcov.


  coverage:
    name: Coverage
    runs-on: ubuntu-latest

    steps:
      # ...

      - name: Generate coverage
        working-directory: ${{github.workspace}}/build
        run: |
          sudo apt-get install lcov -y
          lcov --capture --directory . --output-file coverage.info --ignore-errors mismatch
          lcov --remove coverage.info "*/usr/*" -o coverage.info

      - name: Upload coverage reports to Codecov
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ${{github.workspace}}/build/coverage.info

It depends on your situation if you need --ignore-errors mismatch and lcov --remove.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.