Update docs: Documented a better way to detect if a code is running from within a pytest run

This commit is contained in:
dheerajck 2024-03-22 23:51:58 +05:30 committed by Bruno Oliveira
parent 2e1dfb0c16
commit 0fdbb5f904
1 changed files with 4 additions and 16 deletions

View File

@ -409,25 +409,13 @@ running from a test you can do something like this:
.. code-block:: python .. 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 if _called_from_test_by_pytest:
# called from within a test run by pytest
# 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
... ...
else: else:
# called "normally" # called "normally"