Search code examples
pythonpytestgithub-actionspytest-cov

Github Actions Pytest fail process along with saving it to txt file


I am trying to set up workflow, which runs tests and comments test coverage. To achieve that, I need to save pytest call results into txt file. However, by doing so with following command:

Run pytest --cov=./ --junitxml=pytest.xml --capture=tee-sys --cov-report=term-missing:skip-covered tests/test_app.py | tee pytest-coverage.txt

I run into a problem, when if some tests fail whole workflow finishes anyway. What I want is workflow failing if any of the tests fail, otherwise I will use pytest-coverage.txt to print code coverage.


Solution

  • You might need to set pipefail:

    If set, the return value of a pipeline is the value of the last (rightmost) > command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default.

    So the workflow step should be:

    run: |
      set -o pipefail
      pytest --cov=./ \
             --junitxml=pytest.xml \
             --capture=tee-sys \
             --cov-report=term-missing:skip-covered \
             tests/test_app.py | \
      tee pytest-coverage.txt
    

    Notes:

    • In cloud runners this shouldn't be necessary but it seems to be an issue in self-hosted runners.
    • Alternatively it could also be set by modifying shell in job defaults.