--- id: lib-detect-testenv version: "3.0.4" license: MIT license_treatment: permissive maintenance: active --- # lib-detect-testenv — Detect test environment - pytest, doctest, unittest, or regular execution License: permissive · Maintenance: active · Downloads: 2.6M/mo ## 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 above — 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 pip install lib-detect-testenv uv add lib-detect-testenv 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_current - Install friction: low - Maintenance: active - Downloads: 2.6M/month (top 5,000 on PyPI) - Known vulnerabilities: none known ## Tags detect pytest execution, test environment detection, doctest runtime check, is code running in tests, pytest doctest setup.py detection, test runner identification, conditional test environment logic, test-detection, runtime-inspection [View on SkillFed](https://skillfed.io/packages/lib-detect-testenv) · [View on PyPI](https://pypi.org/project/lib-detect-testenv/)