Running PyTest in GitHub Actions helps you automatically test your Python code every time you make changes. This keeps your code working well without manual checks.
Running PyTest in GitHub Actions
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
PyTest
name: Python PyTest Workflow
on: [push, pull_request]
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: pytestThis YAML file defines a GitHub Actions workflow to run PyTest.
It triggers on code pushes and pull requests.
Examples
PyTest
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.9'
- run: pip install pytest
- run: pytestPyTest
on:
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- run: pip install -r requirements.txt
- run: pytestSample Program
This workflow runs PyTest automatically on every push or pull request using Python 3.12.
PyTest
name: Python PyTest Workflow
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
- name: Run tests
run: pytestImportant Notes
Make sure your test files and functions follow PyTest naming conventions (files start with test_, functions start with test_).
You can add more dependencies to the install step if your project needs them.
Use runs-on: ubuntu-latest to run tests on a Linux machine in GitHub Actions.
Summary
GitHub Actions can run PyTest automatically on code changes.
Define a workflow YAML file with steps to set up Python, install PyTest, and run tests.
This helps catch errors early and keeps your code healthy.
Practice
1. What is the main purpose of running
pytest in a GitHub Actions workflow?easy
Solution
Step 1: Understand pytest's role in GitHub Actions CI
PyTest is a testing tool that checks if code works correctly. GitHub Actions runs tests automatically on code changes to catch errors early.Final Answer:
To automatically test code changes and catch errors early -> Option AQuick Check:
PyTest in GitHub Actions = automatic testing [OK]
Hint: PyTest in GitHub Actions runs tests automatically [OK]
Common Mistakes:
- Confusing testing with deployment
- Thinking PyTest formats code
- Assuming it creates backups
2. Which of the following is the correct step to install PyTest in a GitHub Actions workflow YAML file?
easy
Solution
Step 1: Identify the package manager and correct install command for PyTest
PyTest is a Python package installed usingpip. The correct command isrun: pip install pytest.Final Answer:
run: pip install pytest -> Option CQuick Check:
Python packages use pip install [OK]
Hint: Use pip to install Python packages like PyTest [OK]
Common Mistakes:
- Using npm which is for JavaScript
- Using apt-get or brew which are system package managers
- Missing the pip command
3. Given this GitHub Actions step in a workflow YAML file:
What will happen when this step runs?
- name: Run tests
run: pytest tests/What will happen when this step runs?
medium
Solution
Step 1: Analyze the pytest tests/ command and default behavior
pytest tests/runs all test files inside thetests/folder by default but does not install dependencies or deploy code.Final Answer:
PyTest will run all tests inside thetests/directory -> Option DQuick Check:
pytest tests/ runs tests in tests/ folder [OK]
Hint: pytest <folder> runs tests in that folder [OK]
Common Mistakes:
- Thinking PyTest installs dependencies
- Assuming PyTest deploys code
- Expecting automatic report file creation
4. You wrote this GitHub Actions step:
But the workflow fails with "command not found" error. What is the likely cause?
- name: Run tests run: py.test
But the workflow fails with "command not found" error. What is the likely cause?
medium
Solution
Step 1: Diagnose the command not found error for py.test
PyTest must be installed first; bothpytestandpy.testwork if installed.Final Answer:
PyTest is not installed before running the tests -> Option BQuick Check:
Install PyTest before running tests [OK]
Hint: Always install PyTest before running it in workflow [OK]
Common Mistakes:
- Assuming command spelling is wrong
- Ignoring PyTest installation step
- Blaming GitHub Actions for test failures
5. You want to create a GitHub Actions workflow that runs PyTest on Python 3.10 and 3.11 versions. Which YAML snippet correctly sets up the Python versions and runs PyTest?
hard
Solution
Step 1: Identify YAML with matrix strategy and correct step order for multi-version testing
Usestrategy.matrixto run on 3.10/3.11; checkout, then setup Python with${{ matrix.python-version }}, install pytest, run tests.Final Answer:
YAML snippet with matrix strategy and correct step order -> Option AQuick Check:
Matrix + setup-python + install + run = jobs: test: runs-on: ubuntu-latest strategy: matrix: python-version: [3.10, 3.11] steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: pip install pytest - name: Run tests run: pytest [OK]
Hint: Use matrix for versions, setup-python before install and test [OK]
Common Mistakes:
- Not using matrix for multiple versions
- Installing PyTest before setting Python version
- Repeating setup-python steps instead of matrix
