Skip to main content

Pytest plugin to restrict the test types allowed

Project description

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.

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

pytest_restrict-4.9.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

pytest_restrict-4.9.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

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

Hashes for pytest_restrict-4.9.0.tar.gz
Algorithm Hash digest
SHA256 5ed5e59901b159c21d8c0931904a317dce7cc604d577a490417a51bb9d58394f
MD5 06580fe39c7044e5a313843340cdaee5
BLAKE2b-256 2552f7475a3d2a0e718a0c72854f067d1131429611b69161dfa8ee33ed0070bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_restrict-4.9.0.tar.gz:

Publisher: main.yml on adamchainz/pytest-restrict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_restrict-4.9.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_restrict-4.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6abb11c16bb726bee7c09bfac0e730208253bec57193a69df827baa59776bdec
MD5 7c223cc35500a67d809222d65b5cc4e1
BLAKE2b-256 4dbb543b36eaa9c5e17ce99e43442c1d61cfe410ea345397f7e95dc566e2461f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_restrict-4.9.0-py3-none-any.whl:

Publisher: main.yml on adamchainz/pytest-restrict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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