Search code examples
code-coveragegithub-actionscoverage.py

In coverage.py, how to get minimum file coverage?


I am running coverage with this line, where $FILES is list of modified file in the current pull request.

coverage run --include=$FILES -m pytest tests -vv -rf -x

This works fine, however this is the result I get

Name               Stmts   Miss  Cover
--------------------------------------
src/database.py      153     24    84%
src/endpoints.py      94     20    79%
src/models.py        184      0   100%
--------------------------------------
TOTAL                431     44    90%

As you can see, the TOTAL field is the average, which is 90%, however my desired check is to make sure the minimum file coverage is over 80%, which endpoints.py fails at. How do I get this info and check that?

I am running coverage for github action and this is my relevant action file:

   - name: Run pytest
      if: env.file_size > 1
      run: |
        FILES=$(sed 's/ /,/g' <<< "${{ steps.files.outputs.files_updated }} ${{ steps.files.outputs.files_created }}")
        echo "Files: $FILES"
        coverage run --include=$FILES -m pytest tests -vv -rf -x
      continue-on-error: true
    - name: Build coverage report
      if: env.file_size > 1
      run: |
        COVERAGE=$(coverage report --include=$FILES)
        echo "$COVERAGE"
        echo 'COVERAGE<<EOF' >> $GITHUB_ENV
        echo  "$COVERAGE"  >> $GITHUB_ENV
        echo 'EOF' >> $GITHUB_ENV
    - name: Get Coverage %
      if: env.file_size > 1
      run: |
        LAST_LINE=$(tail -1 <<< "${{ env.COVERAGE }}")
        SCORE=$(sed 's/.*[[:space:]]\([0-9]\+\)%/\1/' <<< "$LAST_LINE")
        echo "Modified file code coverage score is $SCORE"
        echo 'SCORE<<EOF' >> $GITHUB_ENV
        echo  "$SCORE"  >> $GITHUB_ENV
        echo 'EOF' >> $GITHUB_ENV

Solution

  • Coverage.py itself doesn't offer the minimum file coverage. You can get the data in JSON form and then find it.

    I wrote a proof-of-concept for assessing goals like this: https://nedbatchelder.com/blog/202111/coverage_goals.html