You've written some unit tests for your Python app. Good for you! There are dozens of us, dozens! You don't always remember to run your tests, or worse, your colleagues don't always remember to run them.
Wouldn't it be nice to automatically run unit tests on every commit to GitHub? What about on every pull request? You can do this with GitHub Actions. You'd be able to hunt down commits that broke the build, and if you're feeling blamey, who broke the build. Sounds complicated, but it's not. Sounds like it might cost money, but the free version has ~30 hours of execution per month. Let me show you how to set this up.
There is example code for this blog post here.
I'm going to assume that:
If you're already running unit tests locally you can skip this section. Otherwise, your Python project's folder looks something like this:
. ├── env Python virtualenv ├── requirements.txt Python requirements ├── README.md Project description └── stuff.py Your code
If you don't have tests already, I recommend trying pytest (and adding it to your requirements.txt).
pip install pytest
You'll need at least one test
# test_stuff.py from stuff import run_stuff def test_run_stuff(): result = run_stuff() assert result == 1
You'll want to make sure your tests run and pass locally
You'll need to create new a file in a new folder:
You can learn more about these config files here.
Here's an example file:
name: Project Tests on: push: branches: - master pull_request: branches: - master jobs: build: runs-on: ubuntu-latest steps: - uses: actions/[email protected] - name: Set up Python 3.6 uses: actions/[email protected] with: python-version: 3.6 - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt - name: Test with pytest run: pytest -vv
Now your project looks like this:
. ├── .github GitHub hidden folder | └── workflows Some other folder | └── ci.yml GitHub Actions config ├── env Python virtualenv ├── requirements.txt Python requirements ├── README.md Project description ├── test_stuff.py pytest unit tests └── stuff.py Your code
Commit your changes, push it up to GitHub and watch your tests run!
Sometimes they fail:
Sometimes they pass:
You can add a "badge" to your project's README.md. Assuming your project was hosted at https://github.com/MyName/my-project/, you can add this to your README.md file:
If you have any feedback or questions email me at [email protected]