This is Day 20 of the #100DaysOfPython challenge.
The final project code can be found on my GitHub repo.
- Familiarity with Pipenv. See here for my post on Pipenv.
- Read Python Unit Testing With PyTest if you are unfamiliar with Python.
Let’s clone the original PyTest repo in
At this stage, we can test that our repo is working correctly with PyTest locally:
At this stage, we are ready to set things up for our GitHub action!
Adding the test script
In of the
Pipfile, we want to add a
[scripts] section to add a
test script will simply call
pytest. We will use this script within our GitHub action.
In order to also prepare our GitHub action to give us more vebose information, we will also be passing a
-v flag to our call to
We can test things are working as expected by running
pipenv run test -v from our terminal:
The verbose flag gives us more information about which test ran and passed. This can be helpful for debugging in CI.
Adding the GitHub action
We are now ready to add the GitHub action. Within the
.github/workflows/pytest.yml file that we created earlier, add the following:
Here we are doing a couple of things:
- Creating a job call
- Running this job on a
pushevent to the repository.
- Running the job on
- Setting a custom timeout of 10 minutes (albeit this is overkill, feel free to omit).
- Setting up the Python environment for the latest in version
- Install dependencies with a cache set to be the hash of the lockfile.
- Running the test suite that we setup the command and tested before.
That is all that we need for this repo to be working!
Be sure at this stage that you have set up your own origin remote for the repo.
At this stage, all we need to do is commit the code and push the repo and the job will be available under the actions tab in the GitHub UI.
Today’s post demonstrated how to use GitHub actions to test Python code on a push to the remote repository. We used the
pytest testing framework to test our code.
Resources and further reading
- The ABCs of Pipenv
- GitHub Actions
- Creating a Python GitHub Action
- Final project
- Python Unit Testing With PyTest
Originally posted on my blog. To see new posts without delay, read the posts there and subscribe to my newsletter.