Search code examples
pythoncode-coveragetravis-ci

How to get travis to fail if tests do not have enough coverage for python


I it possible to have travis fail if my test don't have enough coverage, say < 90% for example.

Normally I run my tests with the following travis config entry.

script:
 - coverage run --source="mytestmodule" setup.py test

Solution

  • According to this link, if you add the --fail-under switch to the coverage report command, it will exit with a non-zero exit code (which travis will see as a failure) if the code coverage is below the given percentage.

    That would make the script section of your .travis.yml file look like:

    script
     - coverage run --source="mytestmodule" setup.py test
     - coverage report --fail-under=80
    

    Of course you could replace 80 with whatever percentage you'd like.