0
0
PytestHow-ToBeginner ยท 4 min read

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 push or pull_request.
  • jobs: Defines tasks to run, usually a test job.
  • 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 pytest without 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@v3 to get your code.
  • Use actions/setup-python@v4 to specify your Python version.
  • Install dependencies with pip install -r requirements.txt or directly pip install pytest.
  • Run pytest with the correct path to your tests.
  • Trigger workflows on push and pull_request for 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.