Detect Test Environment

3.0.1 · active · verified Sat Apr 11

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.

Warnings

Install

Imports

Quickstart

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")

view raw JSON →