Pytest plugin to restrict the test types allowed
Project description
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.
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 pytest_restrict-4.9.0.tar.gz.
File metadata
- Download URL: pytest_restrict-4.9.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ed5e59901b159c21d8c0931904a317dce7cc604d577a490417a51bb9d58394f
|
|
| MD5 |
06580fe39c7044e5a313843340cdaee5
|
|
| BLAKE2b-256 |
2552f7475a3d2a0e718a0c72854f067d1131429611b69161dfa8ee33ed0070bc
|
Provenance
The following attestation bundles were made for pytest_restrict-4.9.0.tar.gz:
Publisher:
main.yml on adamchainz/pytest-restrict
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_restrict-4.9.0.tar.gz -
Subject digest:
5ed5e59901b159c21d8c0931904a317dce7cc604d577a490417a51bb9d58394f - Sigstore transparency entry: 934484158
- Sigstore integration time:
-
Permalink:
adamchainz/pytest-restrict@b2f285628bd72d43078233b7f56da2a3ea7069eb -
Branch / Tag:
refs/tags/4.9.0 - Owner: https://github.com/adamchainz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@b2f285628bd72d43078233b7f56da2a3ea7069eb -
Trigger Event:
push
-
Statement type:
File details
Details for the file pytest_restrict-4.9.0-py3-none-any.whl.
File metadata
- Download URL: pytest_restrict-4.9.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6abb11c16bb726bee7c09bfac0e730208253bec57193a69df827baa59776bdec
|
|
| MD5 |
7c223cc35500a67d809222d65b5cc4e1
|
|
| BLAKE2b-256 |
4dbb543b36eaa9c5e17ce99e43442c1d61cfe410ea345397f7e95dc566e2461f
|
Provenance
The following attestation bundles were made for pytest_restrict-4.9.0-py3-none-any.whl:
Publisher:
main.yml on adamchainz/pytest-restrict
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_restrict-4.9.0-py3-none-any.whl -
Subject digest:
6abb11c16bb726bee7c09bfac0e730208253bec57193a69df827baa59776bdec - Sigstore transparency entry: 934484172
- Sigstore integration time:
-
Permalink:
adamchainz/pytest-restrict@b2f285628bd72d43078233b7f56da2a3ea7069eb -
Branch / Tag:
refs/tags/4.9.0 - Owner: https://github.com/adamchainz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@b2f285628bd72d43078233b7f56da2a3ea7069eb -
Trigger Event:
push
-
Statement type: