Skip to main content

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>.py per migration
  • 🧪 Pre-filled test classes using Django's MigrationExecutor for 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

  1. Discovery: Scans all Django apps for migration files
  2. Analysis: Extracts metadata (dependencies, operations) using regex parsing
  3. Generation: Creates test files from templates with migration-specific data
  4. Testing: Generated tests use Django's MigrationExecutor for real migration testing

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_migration_testgen-0.1.0.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_migration_testgen-0.1.0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file django_migration_testgen-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django_migration_testgen-0.1.0.tar.gz
Algorithm Hash digest
SHA256 216b97d5320e7c5d1f7b1478415d21d60334d9af838b8144db264a61950ce1db
MD5 4e4fa328997c421dd53635230f28fc49
BLAKE2b-256 ddb1f18e07d9e373edbba94b649b817d0d356962dac32157756264f7a7989b7a

See more details on using hashes here.

File details

Details for the file django_migration_testgen-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_migration_testgen-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c3ff0a08c0e323e36fea07ad28f7193259db748224364616664f5fab11ca50bf
MD5 1b945d91b4f0cfcb4f3da3fc0eb88197
BLAKE2b-256 42cdc2f6d68211634b9758dad1e8d1b5a12dd5d75db1eacc62c8ea713b21cc42

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page