Show a helpful pytest summary on GitHub Actions
Hello pythonists.
As we developers know, testing our code is one of the most important parts of our work. That’s why I’m here to show how I integrate my tests in my GitHub Actions CI/CD workflows, not only by running them, but also by getting a useful summary to understand which tests failed and why, without having to dig into the job logs of the GitHub workflows.
To achieve this goal I develop an easy-to-use action to integrate in your workflows.
Please note that my
pytest-summary
action is just an extension of thetest-sum mary
one.
Example
To follow the example, you need either to create a new repository or to use an existing one
In order to integrate the action in a workflow we need to create some tests to run, so let’s create 2 fake ones, one failing, one succeeding:
def test_false:
assert Falsedef test_true:
assert True
We save this file naming it test_fake.py
in a folder called tests
.
Once we created the tests, we can start to create our CI/CD workflow using the pytest-summary
action.
So let’s create a file called validate.yml
in the .github/workflows
folder:
name: "Validate"on:
workflow_dispatch:jobs:
simple:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main- name: Set up Python
uses: actions/setup-python@main
with:
python-version: "3.10"- name: Run tests
uses: dariocurr/pytest-summary@main
with:
paths: tests/test_fake.py
# or `tests/**`, `tests`
options: --quiet
# keep pytest quite since there is the action summary
show: all
# show the summary for both successful and failed tests
Then let’s push our new files to GitHub.
Once pushed, since we added the workflow_dispatch
option on the on
clause, we are able to run the workflow on demand.
Then, let’s move to the Actions
tab of our repository and select the Validate
workflow on the left-hand list. Thus, click on theRun workflow
button.
After half a minute we should have our result that should look like this
As we can see, since we used the show: all
option, all the tests are reported in the summary.
Those that fails have an additional box showing the traceback of the error, so that it is easy to understand the reason of the failure.
Conclusion
I hope I have shown you the potential of this action and that it can be useful for your pipelines.
To know more about options and configuration, please visit the GitHub repository.