Skip to main content
https://img.shields.io/github/actions/workflow/status/adamchainz/pytest-restrict/main.yml.svg?branch=main&style=for-the-badge https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge https://img.shields.io/pypi/v/pytest-restrict.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

Pytest plugin to restrict the test types allowed.


Testing a Django project? Check out my book Speed Up Your Django Tests which covers loads of ways to write faster, more accurate tests.


Features

This plugin allows you to restrict the test types allowed to ensure they inherit from one of a given list of classes. Useful on projects where you have custom test classes that developers may forget about.

Installation

Install with:

python -m pip install pytest-restrict

Python 3.10 to 3.14 supported.

Usage

Pytest will automatically find the plugin and use it when you run pytest, but by default there are no restrictions. To restrict the test types, set the restrict_types configuration option or the --restrict-types command line option. Either takes a list of import paths to allowed test base classes, passed to pkgutil.resolve_name(), for which you should prefer the form <module.path>:<classname>.

Configuration option

Set the restrict_types option in your pytest configuration file to a list of strings, each being an import path to an allowed test base class. For example, to only allow Django’s test case classes within pyproject.toml:

[tool.pytest]
restrict_types = ["django.test:SimpleTestCase"]

To allow function tests and other non-class test types (such as doctests), add the special string “None”:

[tool.pytest]
restrict_types = ["None", "django.test:SimpleTestCase"]

Command line option

Set the --restrict-types command line option when running pytest to a comma-separated list of import paths to allowed test base classes. For example, to only allow Django’s test case classes:

pytest --restrict-types=django.test:SimpleTestCase

You can set this option in addopts in your pytest configuration file:

[tool.pytest]
addopts = ["--restrict-types=django.test:SimpleTestCase"]

…but in this case, it’s preferable to use the restrict_types configuration option instead.

History

I developed this feature in a closed source Nose plugin whilst working on the big Django project at YPlan. We had some custom enhancements and fixes on top of the Django test classes, but developers sometimes forgot about using them and instead used the built-in unittest classes, or the plain Django ones. Our solution was to just make the test runner error if it encountered non-whitelisted test types.

This package is a pytest port of that plugin.

Release files for pytest-restrict 4.9.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-restrict 4.9.0
File Size Uploaded
pytest_restrict-4.9.0.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-restrict 4.9.0
File Interpreter ABI Platform
pytest_restrict-4.9.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.2 kB

Release files / pytest_restrict-4.9.0.tar.gz

Download URL pytest_restrict-4.9.0.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5ed5e59901b159c21d8c0931904a317dce7cc604d577a490417a51bb9d58394f
BLAKE2b-256 checksum
How to use checksums
2552f7475a3d2a0e718a0c72854f067d1131429611b69161dfa8ee33ed0070bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Feb 9, 2026.

Transparency log

Release files / pytest_restrict-4.9.0-py3-none-any.whl

Download URL pytest_restrict-4.9.0-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6abb11c16bb726bee7c09bfac0e730208253bec57193a69df827baa59776bdec
BLAKE2b-256 checksum
How to use checksums
4dbb543b36eaa9c5e17ce99e43442c1d61cfe410ea345397f7e95dc566e2461f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Feb 9, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

4.9.0 This release

2 release files

4.8.0

2 release files

4.7.0

2 release files

4.6.0

2 release files

4.5.0

2 release files

4.4.0

2 release files

4.3.0

2 release files

4.2.0

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.0.0

2 release files

1.0.0

2 release files

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