Generic run-my-tests-please GitHub Workflow
This post has only one objective. The next person who wants to run the tests on his Python project hosted on GitHub should be able to read it and get his tests running in 15 minutes or less using GitHub Actions.
So, concepts:
- Github Actions: A free (mostly) continuous integration thing GitHub gives you so you can run your tests automatically, so people know your project kinda works.
- Tests: you should have them.
This assumes your project is "modern" in that it uses Poetry for dependency management and packaging. Of course maybe it doesn't, but don't worry, that only is important for maybe 3 lines of YAML.
So, Github Actions runs what's called "workflows". You define these in YAML files
in .github/workflows/whatever.yml
in your repo.
Here's the original version of the workflow we'll be studying today: test.yml
Don't worry about the project it's in, it really doesn't matter.
Now, let's examine that file bit by bit with explanations, so you can see if something needs changing for you.
name: CI
on:
[push, pull_request]
This workflow is called "CI" and it runs on every push to any branch.
jobs:
build:
strategy:
matrix:
python-version: ["3.10"]
runs-on: ubuntu-latest
Put all the versions of Python you want to test in python-versions
. This will run the tests in Ubuntu.
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Switch to Current Branch
run: git checkout ${{ env.BRANCH }}
Checkout this repo we are testing, go to the right branch.
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
Install whatever Python version we are testing.
- name: Install dependencies
run: |
pip install poetry
poetry install
Install whatever you need. Since this project uses poetry, I install poetry and then use poetry to setup things. If you are using something else, then change it accordingly.
- name: Run unit tests
run: |
poetry run pytest
Run the tests in whatever way they need to run. Again, since I am using poetry, this works. If you are not, then change this as needed.
So, put this file in .github/workflows/test.yml
, modify as needed. Commit. Push.
Now your tests run on every push.
And that's it. If your project uses poetry, then this workflow may work unchanged (except maybe for Python versions?)
Good luck!