skillfed

lib-detect-testenv

Detect test environment - pytest, doctest, unittest, or regular execution

lib-detect-testenv v3.0.4 2.6M downloads/30d#2,997 on PyPI0
Permissive license MIT Active released

What it is and what it does

lib-detect-testenv is a runtime detection library that identifies whether your Python code is executing under pytest, doctest (including PyCharm's docrunner), or setup.py test. It works by inspecting sys.argv and execution context to determine the active test runner, allowing you to conditionally enable or disable code paths based on whether tests are running.

The package exports both a Python API (functions like is_testenv_active(), is_pytest_active(), is_doctest_active()) and a command-line interface for shell scripting and CI/CD integration. It uses a three-level exit code system (0 for detected, 1 for not detected, 2 for error) to support programmatic shell usage. The single runtime dependency is rich-click, and it supports Python 3.10 and later on Linux, macOS, and Windows.

Use it for:

  • Conditionally skip expensive initialization or logging in production code when running under pytest.
  • Detect doctest execution to apply different assertion or output formatting rules.
  • Shell scripts and CI/CD pipelines that need to branch behavior based on whether tests are active.
  • Detect setup.py test invocation to apply legacy test runner compatibility logic.
  • Programmatic test environment checks in application startup code to adjust configuration or mocking.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Detects whether code is running in a test environment (pytest, doctest, or setup.py test) via runtime inspection of sys.argv and execution context.

Yes. Low install friction, permissive license, active maintenance, no known vulnerabilities, and a clear single-purpose API make this a straightforward choice if you need runtime test-environment detection. The CLI and Python API both work well for their intended use cases.

Install

lib-detect-testenv on PyPI

pip

pip install lib-detect-testenv

uv

uv add lib-detect-testenv

poetry

poetry add lib-detect-testenv

Installing lib-detect-testenv

Before you install

Low friction: pure Python wheel with a single runtime dependency (rich-click). Active maintenance with a release 14 days ago. Requires Python 3.10 or later.

License in practice

MIT license (permissive) — you can use, modify, and distribute this package freely with minimal restrictions.

Quickstart

pip install lib-detect-testenv

from lib_detect_testenv import is_testenv_active

if is_testenv_active():
    print("Running in test environment")
else:
    print("Running in production")

Requires Python 3.10 or later.

Verify before relying

  • Whether rich-click's CLI integration is required for basic detection functions or optional.
  • Performance characteristics when called repeatedly in tight loops during test execution.

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.10)
Install friction low — pure-Python wheel
Runtime dependencies 1 — rich-click
Maintenance actively maintained — 14 days since the last release
Last repo commit
First released
Downloads 2,568,931/month — #2,997 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: lib_detect_testenv-3.0.4-py3-none-any.whl

Keywords: doctest, pytest, test-detection, test-environment, testing, unittest

Environment :: ConsoleLicense :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: Python :: 3Programming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Typing :: Typed

Tags

detect pytest executiontest environment detectiondoctest runtime checkis code running in testspytest doctest setup.py detectiontest runner identificationconditional test environment logic
test-detectionruntime-inspection

More Testing packages