Auto-generate test files for Django migration files
Project description
Auto-generate test files for Django migration files.
This Django app scans all your apps for migration files and scaffolds migration test templates using MigrationExecutor. Quickly build migration coverage and ensure schema integrity across versions — ideal for CI, large teams, and enterprise projects.
🚀 Features
- 🔍 Scans all apps for migration files automatically
- 📁 Creates organized test files at
tests/migrations/test_<migration_name>.pyper migration - 🧪 Pre-filled test classes using Django's
MigrationExecutorfor real migration testing - ⚙️ Fully integrated with Django's management command system
- ✅ Simple plug-and-play: install, configure, and run
- 🎯 Smart detection: Automatically identifies operation types and dependencies
- 🔄 Rollback testing: Tests both forward and backward migration capabilities
- ⚡ Performance optimized: Uses regex parsing for fast migration analysis
📦 Installation
pip install django-migration-testgen
⚙️ Configuration
Add to your Django settings.py:
INSTALLED_APPS = [
# ... your existing apps
'django_migration_testgen',
]
🎯 Usage
Generate tests for all migrations
python manage.py generate_migration_tests
Generate tests for a specific app
python manage.py generate_migration_tests --app myapp
Preview what would be generated (dry run)
python manage.py generate_migration_tests --dry-run
Force overwrite existing test files
python manage.py generate_migration_tests --force
Custom output directory
python manage.py generate_migration_tests --output-dir /path/to/tests
📁 Generated Test Structure
For a migration myapp/migrations/0001_initial.py, the tool generates:
myapp/
├── migrations/
│ └── 0001_initial.py
└── tests/
└── migrations/
└── test_0001_initial.py # Generated test file
🧪 Generated Test Content
Each generated test file includes:
class Test0001Initial(TestCase):
"""
Test migration: myapp.0001_initial
Migration Details:
- App: myapp
- Migration: 0001_initial
- Dependencies: [('auth', '0001_initial')]
- Operations: CreateModel, AddField
"""
def test_migration_exists(self):
"""Test that the migration file exists and is loadable."""
# Validates migration can be loaded
def test_migration_forward(self):
"""Test forward migration execution."""
# Tests migration applies successfully
# Compares before/after database state
def test_migration_backward(self):
"""Test migration rollback capability."""
# Tests migration can be rolled back
def test_migration_sql_generation(self):
"""Test that migration generates valid SQL."""
# Validates SQL generation without execution
# ... additional test methods
🔧 Customization
Custom Templates
You can create custom test templates by extending TestFileGenerator:
from django_migration_testgen.utils import TestFileGenerator
class CustomTestFileGenerator(TestFileGenerator):
def generate_test_file(self, migration_info, output_dir):
# Your custom generation logic
pass
Custom Scanning
Extend MigrationScanner for custom migration discovery:
from django_migration_testgen.utils import MigrationScanner
class CustomMigrationScanner(MigrationScanner):
def get_filtered_migrations(self, **filters):
# Your custom filtering logic
pass
🎯 Testing Your Migrations
Run the generated tests with your normal test runner:
# Run all migration tests
python manage.py test tests.migrations
# Run specific migration test
python manage.py test tests.migrations.test_0001_initial
# Run with coverage
coverage run manage.py test tests.migrations
coverage report
📊 CI/CD Integration
Perfect for continuous integration:
# .github/workflows/test.yml
- name: Generate and run migration tests
run: |
python manage.py generate_migration_tests
python manage.py test tests.migrations --verbosity=2
🏗️ How It Works
- Discovery: Scans all Django apps for migration files
- Analysis: Extracts metadata (dependencies, operations) using regex parsing
- Generation: Creates test files from templates with migration-specific data
- Testing: Generated tests use Django's
MigrationExecutorfor real migration testing
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_migration_testgen-0.1.0.tar.gz.
File metadata
- Download URL: django_migration_testgen-0.1.0.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
216b97d5320e7c5d1f7b1478415d21d60334d9af838b8144db264a61950ce1db
|
|
| MD5 |
4e4fa328997c421dd53635230f28fc49
|
|
| BLAKE2b-256 |
ddb1f18e07d9e373edbba94b649b817d0d356962dac32157756264f7a7989b7a
|
File details
Details for the file django_migration_testgen-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_migration_testgen-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3ff0a08c0e323e36fea07ad28f7193259db748224364616664f5fab11ca50bf
|
|
| MD5 |
1b945d91b4f0cfcb4f3da3fc0eb88197
|
|
| BLAKE2b-256 |
42cdc2f6d68211634b9758dad1e8d1b5a12dd5d75db1eacc62c8ea713b21cc42
|