π― Introduction
In the world of modern software development, automated testing and continuous integration are no longer optional β theyβre essential. That's where GitHub Actions shines: a powerful CI/CD tool built directly into GitHub.
In this article, weβll explore how GitHub Actions works as a Testing Management Tool, automate testing for a Python project, and demonstrate how to streamline code quality checks using real-world code examples.
β A complete public repo accompanies this article: github.com/srg-cp/gh-actions-python-tests
π§° What is GitHub Actions?
GitHub Actions is GitHubβs native CI/CD tool that lets you automate workflows across the development lifecycle: from testing and building to deployment.
Why use it as a Testing Management Tool?
- Seamlessly integrates with your GitHub repo.
- Supports all testing frameworks (pytest, unittest, etc.).
- Automates testing on every push or PR.
- Offers clear visibility via workflow status and logs.
π¦ Project Overview
Weβll use a simple Python project:
- A module with a math function (
calculator.py
) - Unit tests using unittest (
test_calculator.py
) - A GitHub Actions workflow to run the tests automatically
π Project structure:
gh-actions-python-tests/
β
βββ calculator/
β βββ calculator.py
βββ tests/
β βββ test_calculator.py
βββ .github/
β βββ workflows/
β βββ test.yml
βββ requirements.txt
βββ README.md
π§ͺ 1. The Python Code
calculator.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
test_calculator.py
import unittest
from calculator.calculator import add, subtract
class TestCalculator(unittest.TestCase):
def test_add(self):
self.assertEqual(add(3, 2), 5)
def test_subtract(self):
self.assertEqual(subtract(10, 4), 6)
if __name__ == '__main__':
unittest.main()
requirements.txt
unittest2
βοΈ 2. GitHub Actions Workflow
Weβll create a CI workflow to automatically install dependencies and run tests on each push or pull_request.
.github/workflows/test.yml
name: Python Unit Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.11
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run unit tests
run: python -m unittest discover -s tests
β This will:
- Trigger on push/PR
- Use Python 3.11
- Run tests with
unittest
π 3. Workflow in Action
Once pushed to GitHub, go to the "Actions" tab to see:
- Live logs of each CI run
- Summary of passed/failed tests
- Direct links to PRs or commits related to the run
This ensures that:
- Developers are alerted to test failures immediately.
- Every change is automatically validated.
- Test coverage becomes part of the development process.
π Real-world Examples from the Community
To reinforce the relevance of GitHub Actions as a testing management tool, here are some public repositories by developers using it with Python:
Repository: Description (Link)
- pallets/flask: Official Flask repository. Uses GitHub Actions to run tests with tox and multiple Python versions. π GitHub
- tiangolo/fastapi: FastAPI framework by SebastiΓ‘n RamΓrez. Contains automated testing workflows. π GitHub
- psf/requests: The famous HTTP library requests uses GitHub Actions to test on all major platforms. π GitHub
- pytest-dev/pytest: The pytest testing framework runs all its test automation through GitHub Actions. π GitHub
- kennethreitz/pipenv: Manages virtual environments and dependencies; runs tests with GitHub Actions. π GitHub
These repositories show how powerful and widely adopted GitHub Actions has become for testing management in Python ecosystems.
π Final Thoughts
GitHub Actions allows for rapid feedback, automated validation, and easy integration β making it a robust and developer-friendly Testing Management Tool.
π Donβt forget to explore the repo here:
π Repo github
Top comments (0)