lib-detect-testenv
Detect test environment - pytest, doctest, unittest, or regular execution
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-testenvuv
uv add lib-detect-testenvpoetry
poetry add lib-detect-testenvInstalling 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
Tags
More Testing packages
Pluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
virtualenvvirtualenv creates isolated Python environments…
permissive · top 100 on PyPI
coverageCoverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
pytest-asynciopytest-asyncio is a pytest plugin that enables…
permissive · top 1,000 on PyPI
pytest-json-ctrfA pytest plugin that generates test reports in…
permissive · top 1,000 on PyPI
detect-test-pollutionDetects test pollution—failures caused by side…
permissive · top 15,000 on PyPI
pytest-markdown-docsA pytest plugin that executes Python code…
permissive · top 15,000 on PyPI
xdoctestXdoctest finds and executes test code embedded…
permissive · top 5,000 on PyPI
pytest-ignore-test-resultsA pytest plugin that selectively ignores test…
permissive · top 15,000 on PyPI
detect_agentDetects whether code is running in an AI agent…
unclear · top 15,000 on PyPI
rootpathDetects a Python project or package root…
permissive · top 15,000 on PyPI
pytest-raceProvides a pytest fixture that runs test…
permissive · top 15,000 on PyPI
deadcodeScans Python codebases to find and optionally…
agpl · top 15,000 on PyPI
pytest-doctestplusA pytest plugin that runs doctests in Python…
permissive · top 15,000 on PyPI
pytest-textual-snapshotA pytest plugin that captures SVG screenshots…
permissive · top 15,000 on PyPI