{"id":4078,"library":"lib-detect-testenv","title":"Detect Test Environment","description":"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.","status":"active","version":"3.0.1","language":"en","source_language":"en","source_url":"https://github.com/bitranox/lib_detect_testenv.git","tags":["doctest","pytest","test-detection","test-environment","testing","unittest"],"install":[{"cmd":"pip install lib-detect-testenv","lang":"bash","label":"Install stable version"},{"cmd":"pip install --upgrade lib-detect-testenv[test]","lang":"bash","label":"Install with test dependencies (optional)"}],"dependencies":[{"reason":"Required runtime environment.","package":"Python","version":">=3.9"}],"imports":[{"symbol":"is_testenv_active","correct":"from lib_detect_testenv import is_testenv_active"},{"symbol":"is_pytest_active","correct":"from lib_detect_testenv import is_pytest_active"},{"symbol":"is_doctest_active","correct":"from lib_detect_testenv import is_doctest_active"},{"symbol":"is_setup_test_active","correct":"from lib_detect_testenv import is_setup_test_active"}],"quickstart":{"code":"from lib_detect_testenv import is_testenv_active\n\n# Auto-detect from sys.argv\nif is_testenv_active():\n    print(\"Test environment detected\")\nelse:\n    print(\"Not in a test environment\")\n\n# Or pass a custom argument string for specific checks\nif is_testenv_active(arg_string=\"pytest my_module.py\"):\n    print(\"Pytest environment detected via custom arguments\")\n\nif is_doctest_active():\n    print(\"Doctest environment detected\")\n","lang":"python","description":"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."},"warnings":[{"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`).","message":"The 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"While 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Functions 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}