Table of Contents
Implementing continuous testing in Django projects is essential for maintaining code quality and ensuring smooth deployment processes. GitHub Actions provides a powerful platform to automate testing workflows seamlessly. This tutorial guides you through setting up continuous testing with GitHub Actions in your Django project.
Prerequisites
- A GitHub account with access to your Django project repository
- Basic knowledge of GitHub Actions and YAML syntax
- Python and Django installed locally for initial setup
Step 1: Prepare Your Django Project
Ensure your Django project has a test suite configured using Django's built-in testing framework. Your tests should be located in an app directory under tests modules or similar. Run your tests locally to verify they work correctly before automating.
Update your requirements.txt file to include all dependencies needed for testing, such as Django, pytest, or other testing tools.
Step 2: Create a GitHub Workflow File
In your repository, create a new directory named .github/workflows if it doesn't exist. Inside, add a new YAML file, for example, ci.yml.
Open ci.yml and define your workflow configuration:
name: Django CI
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:13
ports:
- 5432:5432
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: testdb
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Set up database
run: |
python manage.py makemigrations
python manage.py migrate
- name: Run tests
run: |
python manage.py test
Step 3: Configure Database Settings
Modify your settings.py to allow database configuration via environment variables, ensuring compatibility with GitHub Actions environment:
import os
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.getenv('POSTGRES_DB', 'mydatabase'),
'USER': os.getenv('POSTGRES_USER', 'user'),
'PASSWORD': os.getenv('POSTGRES_PASSWORD', 'password'),
'HOST': 'localhost',
'PORT': '5432',
}
}
Step 4: Push Your Changes and Test Workflow
Commit and push your .github/workflows/ci.yml and any other changes to your repository:
git add .
git commit -m "Add GitHub Actions workflow for Django testing"
git push origin main
Navigate to the GitHub Actions tab in your repository. You should see the workflow running. If successful, your tests will execute automatically on each push or pull request to the main branch.
Additional Tips
- Customize the workflow to include other services like Redis or Elasticsearch if needed.
- Use secrets for sensitive data such as API keys or passwords.
- Extend the workflow to include code linting or deployment steps.
Automating testing with GitHub Actions streamlines your development process, catches bugs early, and maintains high code quality in your Django projects.