Metadata-Version: 2.4
Name: pytest-restrict
Version: 4.9.0
Summary: Pytest plugin to restrict the test types allowed
Author-email: Adam Johnson <me@adamj.eu>
License-Expression: MIT
Project-URL: Changelog, https://github.com/adamchainz/pytest-restrict/blob/main/CHANGELOG.rst
Project-URL: Funding, https://adamj.eu/books/
Project-URL: Repository, https://github.com/adamchainz/pytest-restrict
Keywords: class,pytest,restrict
Classifier: Development Status :: 5 - Production/Stable
Classifier: Framework :: Pytest
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/x-rst
License-File: LICENSE
Requires-Dist: pytest
Dynamic: license-file

===============
pytest-restrict
===============

.. image:: https://img.shields.io/github/actions/workflow/status/adamchainz/pytest-restrict/main.yml.svg?branch=main&style=for-the-badge
   :target: https://github.com/adamchainz/pytest-restrict/actions?workflow=CI

.. image:: https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge
   :target: https://github.com/adamchainz/pygments-git/actions?workflow=CI

.. image:: https://img.shields.io/pypi/v/pytest-restrict.svg?style=for-the-badge
   :target: https://pypi.org/project/pytest-restrict/

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge
   :target: https://github.com/psf/black

.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=for-the-badge
   :target: https://github.com/pre-commit/pre-commit
   :alt: pre-commit

Pytest plugin to restrict the test types allowed.

----

**Testing a Django project?**
Check out my book `Speed Up Your Django Tests <https://adamchainz.gumroad.com/l/suydt>`__ 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:

.. code-block:: bash

    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>``.

.. |pkgutil.resolve_name()| replace:: ``pkgutil.resolve_name()``
__ https://docs.python.org/3/library/pkgutil.html#pkgutil.resolve_name

Configuration option
--------------------

Set the ``restrict_types`` option in your `pytest configuration file <https://docs.pytest.org/en/stable/reference/customize.html>`__ 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 <https://docs.djangoproject.com/en/stable/topics/testing/tools/#provided-test-case-classes>`__ within ``pyproject.toml``:

.. code-block:: 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”:

.. code-block:: toml

    [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 <https://docs.djangoproject.com/en/stable/topics/testing/tools/#provided-test-case-classes>`__:

.. code-block:: bash

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

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

.. code-block:: toml

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

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

.. |addopts| replace:: ``addopts``
__ https://docs.pytest.org/en/latest/reference/reference.html#confval-addopts

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.
