Registry / testing / lib-detect-testenv

lib-detect-testenv

JSON →
library3.0.4pypypi✓ verified 23d ago

The `lib-detect-testenv` library provides utility functions to programmatically detect whether Python code is currently running within a test environment. It supports detection of `pytest`, `doctest` (including PyCharm's docrunner), and `setup.py test` execution contexts. The library also offers a Command Line Interface (CLI) for integration into shell scripts and CI/CD pipelines. The current version is 3.0.1, last released on December 15, 2025, indicating active maintenance.

pip install lib-detect-testenv
INSTALL
IMPORT
SIG · LIB-DETECT-TESTENV
L
lib-detect-testenv
testingpythonv3.0.4
Install
2.9s avg
Import
23ms
Disk
31MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.0.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.024s · 32.1MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.9s · import 0.022s · 33MB
31MB installed
● package 31MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

is_testenv_active
from lib_detect_testenv import is_testenv_active
is_pytest_active
from lib_detect_testenv import is_pytest_active
is_doctest_active
from lib_detect_testenv import is_doctest_active
is_setup_test_active
from lib_detect_testenv import is_setup_test_active

This example demonstrates how to use `is_testenv_active()` to automatically detect if the code is running in any supported test environment (pytest, doctest, setup.py test) based on `sys.argv`. It also shows how to explicitly check for doctest activity or pass a custom `arg_string` for detection.

from lib_detect_testenv import is_testenv_active # Auto-detect from sys.argv if is_testenv_active(): print("Test environment detected") else: print("Not in a test environment") # Or pass a custom argument string for specific checks if is_testenv_active(arg_string="pytest my_module.py"): print("Pytest environment detected via custom arguments") if is_doctest_active(): print("Doctest environment detected")
detect-testenv --version
Debug
Known issues
gotchaThe Command Line Interface (CLI) for `lib-detect-testenv` uses a 3-level exit code system: 0 for 'test environment detected', 1 for 'test environment not detected' (normal negative result), and 2 for 'error occurred'. This is a Unix convention but may be unexpected if only anticipating 0 for success and non-zero for any kind of failure.
fix
Always check for specific exit codes when using the CLI in shell scripts or CI/CD pipelines (e.g., `if [ $? -eq 0 ]; then ... elif [ $? -eq 1 ]; then ... else ... fi`).
affects: All versions
gotchaWhile PyPI tags and some summaries mention 'unittest', the public API functions (`is_pytest_active`, `is_doctest_active`, `is_setup_test_active`) do not include an explicit `is_unittest_active` function. The general `is_testenv_active()` function will detect any supported test environment, including scenarios where a `unittest` runner might invoke Python in a way that matches `setup.py test` or other common patterns. Users specifically looking for `unittest` detection should be aware of this distinction.
fix
Use `is_testenv_active()` for general test environment detection, or manually inspect `sys.argv` for `unittest`-specific patterns if finer-grained control is needed beyond the provided dedicated functions.
affects: All versions
gotchaFunctions like `is_testenv_active` accept an optional `arg_string` parameter. If `None` (the default), the library uses `str(sys.argv)` for detection. The exact content and format of `sys.argv` can vary depending on how a Python script is invoked (e.g., direct execution, via `python -m`, or from a wrapper script), which might affect detection accuracy.
fix
For consistent or specific detection needs, explicitly pass the `arg_string` parameter if `sys.argv` is not reliably structured or if testing different invocation scenarios.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'lib_detect_testenv'
The `lib-detect-testenv` library is not installed in the current Python environment or the environment is not active.
fix
Ensure the library is installed using pip: `pip install lib-detect-testenv` or `python -m pip install lib-detect-testenv`.
ImportError: cannot import name 'is_pytest' from 'lib_detect_testenv'
You are trying to import a function named `is_pytest` which does not exist directly in the `lib_detect_testenv` module; the correct function name is `is_pytest_active`.
fix
Use the correct function name: `from lib_detect_testenv import is_pytest_active`.
AttributeError: module 'lib_detect_testenv' has no attribute 'is_pytest'
You have imported the `lib_detect_testenv` module (e.g., `import lib_detect_testenv`) and are trying to access an attribute or function named `is_pytest` which does not exist; the correct function name is `is_pytest_active`.
fix
Access the correct function name: `lib_detect_testenv.is_pytest_active()`.
lib_detect_testenv: error: unrecognized arguments: --some-invalid-arg
An invalid or unsupported argument was passed to a `lib-detect-testenv` CLI command.
fix
Consult the CLI help for available commands and arguments: `lib_detect_testenv --help` or `lib_detect_testenv [command] --help`.
Upgrade
Version history
3.0.4latest on PyPI · released Jul 31, 2026
Audit
Dependencies
PythonrequiredRequired runtime environment.
Agent activity
1 hits · last 30 days
Resources
lib-detect-testenv — pip install lib-detect-testenv · libregistry