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?branch=main&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.

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.7 to 3.12 supported.


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


Usage

Pytest will automatically find the plugin and use it when you run pytest, however by default there are no restrictions. To restrict the test types, provide --restrict-types as a comma-separated list of import paths to allowed test base classes. The import paths are passed to pkgutil.resolve_name(), for which you should prefer the form <module.path>:<classname>. It’s best to set --restrict-types within addopts in your pytest configuration file.

For example, to restrict tests to Django’s test case classes within pytest.ini:

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

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

[pytest]
addopts = --restrict-types None,django.test:SimpleTestCase

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.5.0.tar.gz (6.3 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.5.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest_restrict-4.5.0.tar.gz.

File metadata

  • Download URL: pytest_restrict-4.5.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pytest_restrict-4.5.0.tar.gz
Algorithm Hash digest
SHA256 fd44355ba2824059601adc3a6acfc96e788a25e861cca8a1dca98bf6d9419970
MD5 454723a79ac73b3fb059377026e05a5b
BLAKE2b-256 02b4701d159767e72d3bd8eaaa609fab09f701a982f6390bbd14722c13a3a5a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_restrict-4.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d422726c01f1853e9fd7b05f896ea8cf65058cebf6323a0e2132feac95151e52
MD5 1e224c124b4f7d187654a0315a41684d
BLAKE2b-256 387784f214a3acb0800e612b8c6bc3d1939d185e52c3a85dbf4dd3295d930c09

See more details on using hashes here.

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