Skip to main content

pytest-django-autocheck

OpenSSF Scorecard

Zero-config pytest plugin that runs generic safety checks on any Django project. .

Pulls in pytest-django automatically and discovers Django settings from your manage.py. Install and run:

pip install pytest-django-autocheck
pytest --autocheck

Why

Legacy or untested Django projects rarely have any safety net. This plugin catches the basics in CI before they reach production:

  • modules that fail to import
  • migrations that don't reverse, or are missing entirely
  • models that can't be saved or stringified
  • admin pages that return 500/404
  • broken URLconfs
  • public pages that return 500
  • template syntax errors
  • management commands that fail to load
  • forms and serializers that can't be instantiated
  • Django system check failures

Requirements

  • Python 3.11+
  • Django 5.0+

Installation

pip install pytest-django-autocheck

Usage

From your project root (where manage.py lives):

pytest --autocheck

The plugin discovers DJANGO_SETTINGS_MODULE from manage.py. If you already declare it explicitly (in pytest.ini/pyproject.toml, via --ds, or as an environment variable), that value wins and auto-discovery is skipped.

Run only specific checks by repeating --autocheck-only:

# a single check
pytest --autocheck --autocheck-only=admin

# multiple checks
pytest --autocheck --autocheck-only=admin --autocheck-only=models

Or skip specific checks with --autocheck-skip:

pytest --autocheck --autocheck-skip=templates --autocheck-skip=serializers

Both flags can be combined; --autocheck-skip is applied after --autocheck-only, so a check named in both ends up skipped. Either flag implies --autocheck, so pytest --autocheck-only=admin is enough.

Valid check names for both flags: admin, forms, imports, management_commands, migrations, models, serializers, system_checks, templates, urls, views.

Checks

Check Description
admin Every admin registered for a project model validates its configuration and renders its changelist and changeform without 500/404. Every admin site mounted in the URLconf is inspected (the default one, a custom-named one, or several side by side); a project without django.contrib.admin, or with no site mounted, has nothing to inspect. Any redirect is reported as WARNING: the client is logged in as a superuser, so a redirect (SSL/www rewrite or a bounce to the login page) means the view was never exercised. Admins for third-party models and for models defined inside a tests package are skipped.
forms Every ModelForm defined in a project app's forms module instantiates without raising.
imports Every module of every project app imports without ImportError or circular import (third-party pip apps are skipped).
management_commands Every management command defined by a project app loads its class and builds its argument parser without raising.
migrations Detects model changes not captured by a migration, statically inspects every project migration for irreversible operations and silent noop-reverse migrations, then dynamically verifies the whole graph by applying it forward, reversing to zero, and re-applying forward on a throwaway database. Third-party and django.contrib migrations are skipped: a dependency's irreversible data migration won't fail your build.
models Every project model is instantiable, savable, and stringifiable via both str() and repr(). Third-party and django.contrib models are skipped, as are test-support models defined inside a tests package (a top-level tests/ or an app-level {app}/tests/).
serializers Every Django REST Framework serializer defined in a project app (<app>.serializers or <app>.api.serializers) binds its fields without raising. Skipped entirely when djangorestframework isn't installed.
system_checks Runs Django's own system check framework (django.core.checks) and reports every error, warning and info message it raises.
templates Every template under the project's own template directories compiles without a TemplateSyntaxError. Only the Django template backend is inspected: Jinja2 templates are not compiled.
urls Every URL pattern loads its included URLconf, resolves its callback, and reverses without raising (other than NoReverseMatch).
views Every named URL that reverses without arguments and whose view is owned by a project app is requested with GET by an unauthenticated client; a raised exception or an HTTP 5xx response is reported. Anything below 500 (redirects, 403, 404) is a legitimate answer for an anonymous client and is ignored. Views shipped by third-party packages are skipped (ownership is resolved from the view callback's module, for both function-based and class-based views), and admin sites (whatever their name) are skipped because the admin check already exercises them with a superuser. Known caveat: a TemplateView.as_view(template_name=...) declared directly in urls.py is skipped, because its view_class is Django's own.

Project-vs-third-party detection is filesystem based: an app is considered third-party when it lives under site-packages, the standard library or the environment prefix. A dependency installed in editable mode (pip install -e) lives outside those locations and is therefore inspected as project code; use the SKIP/CHECKS settings (or MODELS_EXCLUDE) if its findings are noise. When no project app is detected at all (e.g. the project itself is installed in non-editable mode), a warning says so: the checks scoped to project apps would otherwise pass without inspecting anything. The run still passes, on purpose: a project made only of third-party apps is legitimate, so the warning is the only signal. In CI, run the checks against the source tree (or an editable install), not against a copy installed under site-packages.

Settings

Every option is read from Django settings under the PYTEST_DJANGO_AUTOCHECK_ prefix, with a safe default: override only what you need.

Setting Default Effect
PYTEST_DJANGO_AUTOCHECK_CHECKS None List of check names to run, e.g. ["imports", "migrations"]. When unset, every registered check runs. --autocheck-only, when given, takes precedence.
PYTEST_DJANGO_AUTOCHECK_SKIP None List of check names to exclude, e.g. ["templates", "serializers"]. Applied after CHECKS/--autocheck-only. --autocheck-skip, when given, takes precedence.
PYTEST_DJANGO_AUTOCHECK_MIGRATIONS_PROBE_TIMEOUT 300 Max seconds (a positive number, fractions allowed) the dynamic migration probe subprocess may run before it's aborted; on timeout the dynamic step is skipped with a WARNING and the run doesn't fail. The probe self-aborts at this deadline and destroys its throwaway database. On platforms without SIGALRM (e.g. Windows) it can't abort itself: it is killed 30 seconds later and may leave its throwaway database behind, which the WARNING says.
PYTEST_DJANGO_AUTOCHECK_MODELS_EXCLUDE [] List of "app_label.ModelName" labels (case-insensitive) excluded from instance building: the models check skips them entirely and the admin check skips their change view (attribute validation, changelist and add views still run). Escape hatch for models whose domain constraints can never be satisfied by generated data (e.g. a clean() that requires specific related rows, or a CHECK constraint between correlated fields). Every exclusion is reported as INFO, so it never disappears silently.
PYTEST_DJANGO_AUTOCHECK_MODELS_FACTORIES {} Dict mapping "app_label.ModelName" labels (case-insensitive) to the dotted path of a zero-argument callable returning a saved instance, e.g. {"billing.Plan": "billing.testing.make_plan"}. The callable replaces the generic generator for that model in the models and admin checks. A model listed here wins over MODELS_EXCLUDE: providing a factory means the model can be checked after all, so prefer this over excluding it.
PYTEST_DJANGO_AUTOCHECK_DEPLOY False When True, system_checks also runs Django's deployment checks (DEBUG, SECRET_KEY, SSL/HSTS, secure cookies). Off by default since those fail on dev/test settings; enable for a pre-production audit.

The dynamic migration verification runs in a short-lived subprocess, isolated from the suite's own test database. If that subprocess can't set up its environment, the dynamic step is skipped with a warning rather than failing the run. It is skipped the same way when the throwaway database can't be isolated:

  • the project configures more than one database: only default can be isolated, and a data migration routed to another alias would otherwise reach its real database;
  • the database is Oracle, which isolates tests by user and tablespace instead of by database name.

The throwaway database (named after the project's one, with an _autocheck_<pid> suffix) is destroyed whatever the outcome. If destroying it fails, a WARNING names the database left behind.

Instance generation

The models and admin checks need a valid instance of every model. The generator prefers the project's own factory_boy factories when present (looked up in <app>.factories or <app>.tests.factories): they already encode domain rules, which avoids false positives. Falls back to model_bakery when no factory is found. factory_boy is optional; discovery is a no-op if it's not installed. A project factories module that exists but fails to import is reported once, as an ERROR on the module, and no instance is built: generated data never silently replaces factories that broke.

When neither a factory nor generated data can satisfy a model's domain constraints, point PYTEST_DJANGO_AUTOCHECK_MODELS_FACTORIES at your own callable to keep the model checked, or, as a last resort, list it in PYTEST_DJANGO_AUTOCHECK_MODELS_EXCLUDE (see Settings) to exclude it from instance building.

The serializers check requires Django REST Framework and is skipped if it's not installed.

Custom checks

Third-party packages can ship their own checks through the pytest_django_autocheck.checks entry-point group. Each entry point must resolve to a check class (instantiated with no arguments) or a check instance exposing name and run(app_configs) -> list[Finding]:

[project.entry-points."pytest_django_autocheck.checks"]
mycheck = "mypackage.checks:MyCheck"

Registered checks run like the built-in ones: they get their own pytest item, their name works with --autocheck-only/--autocheck-skip and the CHECKS/ SKIP settings, and their ERROR findings fail the run. Check names must be unique: a check reusing the name of a built-in or of another third-party check is rejected. A rejected name, or an entry point that doesn't provide a check, stops the run with a usage error and registers none of the third-party checks.

A check that never touches the database can declare requires_db = False: its pytest item then skips the database fixture, and running only such checks (like the built-in imports, migrations, templates and urls) never creates the test database.

Testing

# clone repository
git clone https://github.com/fabiocaccamo/pytest-django-autocheck.git && cd pytest-django-autocheck

# create virtualenv and activate it
python -m venv .venv && . .venv/bin/activate

# upgrade pip
python -m pip install --upgrade pip

# install package and test requirements
python -m pip install -e ".[test]"

# install pre-commit to run formatters and linters
pre-commit install --install-hooks

# run tests using tox (every environment needs its interpreter installed,
# from Python 3.11 to 3.15; use e.g. `tox -e py313-django52` to run just one)
tox

# or run tests using pytest
coverage run -m pytest && coverage report -m

# run tests against postgres instead of sqlite (defaults shown)
DATABASE_ENGINE=postgres POSTGRES_DB=postgres POSTGRES_USER=postgres \
POSTGRES_PASSWORD=postgres POSTGRES_HOST=localhost POSTGRES_PORT=5432 \
coverage run -m pytest

License

Released under MIT License.


Supporting

See also

  • django-admin-interface - the default admin interface made customizable by the admin itself. popup windows replaced by modals. 🧙 ⚡

  • django-cache-cleaner - clear the entire cache or individual caches easily using the admin panel or management command. 🧹✨

  • django-colorfield - simple color field for models with a nice color-picker in the admin. 🎨

  • django-email-validators - no more invalid or disposable emails in your database. ✉️ ✅

  • django-extra-settings - config and manage typed extra settings using just the django admin. ⚙️

  • django-maintenance-mode - shows a 503 error page when maintenance-mode is on. 🚧 🛠️

  • django-redirects - redirects with full control. ↪️

  • django-treenode - probably the best abstract model / admin for your tree based stuff. 🌳

  • python-benedict - dict subclass with keylist/keypath support, I/O shortcuts (base64, csv, json, pickle, plist, query-string, toml, xml, yaml) and many utilities. 📘

  • python-codicefiscale - encode/decode Italian fiscal codes - codifica/decodifica del Codice Fiscale. 🇮🇹 💳

  • python-fontbro - friendly font operations. 🧢

  • python-fsutil - file-system utilities for lazy devs. 🧟‍♂️

Metadata

Release files for pytest-django-autocheck 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-django-autocheck 0.4.0
File Size Uploaded Provenance Actions
pytest_django_autocheck-0.4.0.tar.gz 55.1 kB With provenance Details for pytest_django_autocheck-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-django-autocheck 0.4.0
File Interpreter ABI Platform Actions
pytest_django_autocheck-0.4.0-py3-none-any.whl Python 3 none any Details for pytest_django_autocheck-0.4.0-py3-none-any.whl

Total release size: 111.6 kB

Release files / pytest_django_autocheck-0.4.0.tar.gz

Download URL pytest_django_autocheck-0.4.0.tar.gz
Size 55.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8719fff2131b437d1da6ef070b176f3a46a70643115b65a11a5736bcd8036d91
BLAKE2b-256 checksum
How to use checksums
68a84806273b036c58434936b8f0082681519ef522ea5682edbe6d5a9a73e32a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / pytest_django_autocheck-0.4.0-py3-none-any.whl

Download URL pytest_django_autocheck-0.4.0-py3-none-any.whl
Size 56.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dc12b6af22edaf321f7ac31bca84d46cff55cfb90b64cb19f12cfe81f3eb628e
BLAKE2b-256 checksum
How to use checksums
c9dc53ac46e253fa23abc8d05a0d3fa1fb60bec9073783b5b0d4ac1696728a44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Security for pytest-django-autocheck 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pytest_django_autocheck-0.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page