adamchainz / pytest-is-running

pytest plugin providing a function to check if pytest is running.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pytest-is-running

https://img.shields.io/github/actions/workflow/status/adamchainz/pytest-is-running/main.yml?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-is-running.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

pytest plugin providing a function to check if pytest is running.

Unmaintained (2024-01-02)

I stopped maintaining this package as it doesn’t provide much value over checking whether pytest has been imported:

import sys

if "pytest" in sys.modules:
    ...

In every project I’ve seen, pytest is only imported when running.


Working on a Django project? Check out my book Boost Your Django DX which covers many ways to improve your development experience. I created pytest-is-running whilst working on the book!


Installation

Install with:

python -m pip install pytest-is-running

Python 3.8 to 3.12 supported.

Usage

pytest will automatically find the plugin and use it when you run pytest. You can check if pytest is running with the is_running() function:

import pytest_is_running


if pytest_is_running.is_running():
    ...

The package avoids importing pytest if it is not running, so that you don’t incur that overhead in non-test paths.

The package registers its plugin hooks as early as possible in pytest’s process, so it should be loaded before any of your non-test modules.

Rationale

This plugin is an alternative to re-implementing the pattern in the pytest documentation. As a plugin, it is loaded earlier than conftest.py or any other code in your project. This makes it a more robust way of checking whether pytest is currently running.

Upstream issue #9502 discusses adding a feature to pytest. It also covers an alternative which is often “good enough” - a simple check if pytest has been imported with:

"pytest" in sys.modules

This won’t be strictly accurate if you happen to import pytest outside of your test run, but that is not very common. You may prefer to using this simpler technique instead of this plugin.

About

pytest plugin providing a function to check if pytest is running.

License:MIT License


Languages

Language:Python 100.0%