Testing Overview
Django-CFG provides zero-configuration test database management. No setup needed — everything works automatically.
What You Get
| Feature | What it does |
|---|---|
| Auto cleanup | Removes stale test databases before each run — no EOFError in CI |
| Extension auto-install | Installs pgvector, pg_trgm, unaccent by scanning your migrations |
| Smart migration handling | Detects and fixes inconsistent migration history automatically |
| Dual runners | SmartTestRunner for PostgreSQL, FastTestRunner for SQLite in-memory |
| Automatic TEST settings | Generates DATABASES[...]['TEST'] config for every database |
Quick Start
python manage.py testThat’s it. Django-CFG automatically:
- Detects you’re running tests
- Removes the old test database (no confirmation prompt)
- Creates a fresh test database
- Installs required PostgreSQL extensions
- Applies all migrations in correct order
- Runs your tests
- Cleans up afterward
Configuration
No changes needed to your config.py:
# config.py — nothing extra required
class MyConfig(DjangoConfig):
project_name = "myapp"
databases = {
"default": DatabaseConfig.from_url("postgresql://user:pass@localhost/myapp")
}Behind the scenes Django-CFG generates the following automatically:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "myapp",
# ... connection settings ...
"TEST": {
"NAME": "test_myapp",
"TEMPLATE": "template0",
"CHARSET": "UTF8",
"CREATE_DB": True,
"MIGRATE": True,
}
}
}
TEST_RUNNER = "django_cfg.testing.runners.SmartTestRunner"Fast Tests with SQLite
Switch all databases to SQLite in-memory for unit tests — no PostgreSQL needed:
python manage.py test --testrunner=django_cfg.testing.runners.FastTestRunnerFastTestRunner is ~10× faster but cannot use PostgreSQL-specific features like pgvector or raw SQL with uuid fields. Use it for pure business logic tests.
Problems Solved
EOFError in CI
Before:
Got an error creating the test database: database "test_myapp" already exists
Type 'yes' if you would like to try deleting the test database, or 'no' to cancel:
EOFError ← no stdin in CIAfter:
Removed old test database: test_myapp
Creating test database for alias 'default'...Missing PostgreSQL Extensions
Before:
django.db.utils.ProgrammingError: type "vector" does not existAfter:
Installed PostgreSQL extensions for test database 'default'Inconsistent Migration History
Before:
InconsistentMigrationHistory: ai_chat.0001_initial applied before
workspaces.0002_alter_workspacemember_optionsAfter:
Removed old test database: test_myapp ← fresh start, correct order
Running migrations... OKCI/CD Integration
GitHub Actions
- name: Run Tests
run: python manage.py test
# No extra configuration needed — SmartTestRunner handles everythingTroubleshooting
Extensions not installing? Check that pgvector is available on the system:
# Ubuntu/Debian
sudo apt-get install postgresql-15-pgvector
# macOS
brew install pgvectorNeed more output?
python manage.py test --verbosity=2See Also
- Test Runners —
SmartTestRunner,FastTestRunner,MigrationManagerAPI - test_db Command — CLI for manual database management
- Migration Internals — deep-dive into what can go wrong
TAGS: testing, test database, zero config, CI/CD, pgvector, SmartTestRunner DEPENDS_ON: [test-runners, test-db-command]