How to Use pytest with GitHub Actions for Automated Testing
To use
pytest with GitHub Actions, create a workflow YAML file in your repository's .github/workflows folder that sets up Python, installs dependencies, and runs pytest. This automates your tests on every push or pull request, ensuring your code stays tested continuously.Syntax
A typical GitHub Actions workflow for pytest includes these parts:
- name: The workflow's name.
- on: Events that trigger the workflow, like
pushorpull_request. - jobs: Defines tasks to run, usually a
testjob. - runs-on: The virtual machine environment, e.g.,
ubuntu-latest. - steps: Series of commands to set up Python, install dependencies, and run tests.
yaml
name: Python pytest CI on: push: branches: [ main ] pull_request: branches: [ main ] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.x' - name: Install dependencies run: | python -m pip install --upgrade pip pip install pytest - name: Run tests run: pytest
Example
This example shows a complete GitHub Actions workflow that runs pytest automatically on pushes and pull requests to the main branch. It checks out your code, sets up Python 3, installs pytest, and runs your tests.
yaml
name: Python pytest CI on: push: branches: [ main ] pull_request: branches: [ main ] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.10' - name: Install dependencies run: | python -m pip install --upgrade pip pip install pytest - name: Run tests run: pytest tests/
Output
============================= test session starts =============================
collected 3 items
tests/test_sample.py ... [100%]
============================== 3 passed in 0.03s ==============================
Common Pitfalls
Common mistakes when using pytest with GitHub Actions include:
- Not specifying the correct Python version in
setup-python. - Forgetting to install dependencies before running tests.
- Running
pytestwithout specifying the test folder or files if your tests are not in the root. - Not checking out the code with
actions/checkout, so the workflow has no code to test.
Always ensure your workflow installs all needed packages and points pytest to the right test directory.
yaml
name: Python pytest CI on: [push] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.10' - name: Run tests without installing dependencies (wrong) run: pytest # Correct way: # Add a step to install dependencies before running pytest
Quick Reference
Tips for using pytest with GitHub Actions:
- Always use
actions/checkout@v3to get your code. - Use
actions/setup-python@v4to specify your Python version. - Install dependencies with
pip install -r requirements.txtor directlypip install pytest. - Run
pytestwith the correct path to your tests. - Trigger workflows on
pushandpull_requestfor continuous testing.
Key Takeaways
Create a GitHub Actions workflow YAML file in .github/workflows to automate pytest runs.
Use actions/checkout and actions/setup-python to prepare the environment before testing.
Always install pytest and other dependencies before running tests in the workflow.
Specify triggers like push and pull_request to run tests automatically on code changes.
Check your test paths and Python version to avoid common errors in the workflow.