From 0fdbb5f904efa93d5e5cbac90f969e3457dd163a Mon Sep 17 00:00:00 2001 From: dheerajck Date: Fri, 22 Mar 2024 23:51:58 +0530 Subject: [PATCH] Update docs: Documented a better way to detect if a code is running from within a pytest run --- doc/en/example/simple.rst | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/doc/en/example/simple.rst b/doc/en/example/simple.rst index 7064f61f0..ca1abc279 100644 --- a/doc/en/example/simple.rst +++ b/doc/en/example/simple.rst @@ -409,25 +409,13 @@ running from a test you can do something like this: .. code-block:: python - # content of your_module.py + import sys - _called_from_test = False + _called_from_test_by_pytest = "pytest" in sys.modules -.. code-block:: python - - # content of conftest.py - - - def pytest_configure(config): - your_module._called_from_test = True - -and then check for the ``your_module._called_from_test`` flag: - -.. code-block:: python - - if your_module._called_from_test: - # called from within a test run + if _called_from_test_by_pytest: + # called from within a test run by pytest ... else: # called "normally"