pytest-restrict


Namepytest-restrict JSON
Version 4.5.0 PyPI version JSON
download
home_pagehttps://github.com/adamchainz/pytest-restrict
SummaryPytest plugin to restrict the test types allowed
upload_time2023-06-16 15:01:03
maintainer
docs_urlNone
authorAdam Johnson
requires_python>=3.7
licenseMIT
keywords pytest restrict class
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ===============
pytest-restrict
===============

.. image:: https://img.shields.io/github/actions/workflow/status/adamchainz/pytest-restrict/main.yml?branch=main&style=for-the-badge
   :target: https://github.com/adamchainz/pytest-restrict/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.

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

----

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

----

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.

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

For example, to restrict tests to Django’s `test case classes <https://docs.djangoproject.com/en/stable/topics/testing/tools/#provided-test-case-classes>`__ within ``pytest.ini``:

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

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

.. code-block:: ini

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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/adamchainz/pytest-restrict",
    "name": "pytest-restrict",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "pytest,restrict,class",
    "author": "Adam Johnson",
    "author_email": "me@adamj.eu",
    "download_url": "https://files.pythonhosted.org/packages/02/b4/701d159767e72d3bd8eaaa609fab09f701a982f6390bbd14722c13a3a5a8/pytest_restrict-4.5.0.tar.gz",
    "platform": null,
    "description": "===============\npytest-restrict\n===============\n\n.. image:: https://img.shields.io/github/actions/workflow/status/adamchainz/pytest-restrict/main.yml?branch=main&style=for-the-badge\n   :target: https://github.com/adamchainz/pytest-restrict/actions?workflow=CI\n\n.. image:: https://img.shields.io/pypi/v/pytest-restrict.svg?style=for-the-badge\n   :target: https://pypi.org/project/pytest-restrict/\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge\n   :target: https://github.com/psf/black\n\n.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=for-the-badge\n   :target: https://github.com/pre-commit/pre-commit\n   :alt: pre-commit\n\nPytest plugin to restrict the test types allowed.\n\nFeatures\n========\n\nThis plugin allows you to restrict the test types allowed to ensure they inherit from one of a given list of classes.\nUseful on projects where you have custom test classes that developers may forget about.\n\nInstallation\n============\n\nInstall with:\n\n.. code-block:: bash\n\n    python -m pip install pytest-restrict\n\nPython 3.7 to 3.12 supported.\n\n----\n\n**Testing a Django project?**\nCheck 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.\n\n----\n\nUsage\n=====\n\nPytest will automatically find the plugin and use it when you run ``pytest``, however by default there are no restrictions.\nTo restrict the test types, provide ``--restrict-types`` as a comma-separated list of import paths to allowed test base classes.\nThe import paths are passed to |pkgutil.resolve_name()|__, for which you should prefer the form ``<module.path>:<classname>``.\nIt\u2019s best to set ``--restrict-types`` within |addopts|__ in your pytest configuration file.\n\n.. |addopts| replace:: ``addopts``\n__ https://docs.pytest.org/en/latest/reference/reference.html#confval-addopts\n\nFor example, to restrict tests to Django\u2019s `test case classes <https://docs.djangoproject.com/en/stable/topics/testing/tools/#provided-test-case-classes>`__ within ``pytest.ini``:\n\n.. |pkgutil.resolve_name()| replace:: ``pkgutil.resolve_name()``\n__ https://docs.python.org/3/library/pkgutil.html#pkgutil.resolve_name\n\n.. code-block:: ini\n\n    [pytest]\n    addopts = --restrict-types django.test:SimpleTestCase\n\nTo allow function tests and other non-class test types (such as doctests), provide the special string \u201cNone\u201d:\n\n.. code-block:: ini\n\n    [pytest]\n    addopts = --restrict-types None,django.test:SimpleTestCase\n\nHistory\n=======\n\nI developed this feature in a closed source Nose plugin whilst working on the big Django project at YPlan.\nWe 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.\nOur solution was to just make the test runner error if it encountered non-whitelisted test types.\n\nThis package is a pytest port of that plugin.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Pytest plugin to restrict the test types allowed",
    "version": "4.5.0",
    "project_urls": {
        "Changelog": "https://github.com/adamchainz/pytest-restrict/blob/main/CHANGELOG.rst",
        "Homepage": "https://github.com/adamchainz/pytest-restrict",
        "Mastodon": "https://fosstodon.org/@adamchainz",
        "Twitter": "https://twitter.com/adamchainz"
    },
    "split_keywords": [
        "pytest",
        "restrict",
        "class"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "387784f214a3acb0800e612b8c6bc3d1939d185e52c3a85dbf4dd3295d930c09",
                "md5": "1e224c124b4f7d187654a0315a41684d",
                "sha256": "d422726c01f1853e9fd7b05f896ea8cf65058cebf6323a0e2132feac95151e52"
            },
            "downloads": -1,
            "filename": "pytest_restrict-4.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1e224c124b4f7d187654a0315a41684d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 4858,
            "upload_time": "2023-06-16T15:01:01",
            "upload_time_iso_8601": "2023-06-16T15:01:01.706000Z",
            "url": "https://files.pythonhosted.org/packages/38/77/84f214a3acb0800e612b8c6bc3d1939d185e52c3a85dbf4dd3295d930c09/pytest_restrict-4.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "02b4701d159767e72d3bd8eaaa609fab09f701a982f6390bbd14722c13a3a5a8",
                "md5": "454723a79ac73b3fb059377026e05a5b",
                "sha256": "fd44355ba2824059601adc3a6acfc96e788a25e861cca8a1dca98bf6d9419970"
            },
            "downloads": -1,
            "filename": "pytest_restrict-4.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "454723a79ac73b3fb059377026e05a5b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 6273,
            "upload_time": "2023-06-16T15:01:03",
            "upload_time_iso_8601": "2023-06-16T15:01:03.198821Z",
            "url": "https://files.pythonhosted.org/packages/02/b4/701d159767e72d3bd8eaaa609fab09f701a982f6390bbd14722c13a3a5a8/pytest_restrict-4.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-16 15:01:03",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "adamchainz",
    "github_project": "pytest-restrict",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "pytest-restrict"
}
        
Elapsed time: 0.10842s