Skip to content

Commit 43ce185

Browse files
committed
Update docs: Documented a better way to detect if a code is running from within a pytest run
1 parent c650e3a commit 43ce185

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

doc/en/example/simple.rst

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -409,25 +409,13 @@ running from a test you can do something like this:
409409

410410
.. code-block:: python
411411
412-
# content of your_module.py
413-
414-
415-
_called_from_test = False
416-
417-
.. code-block:: python
418-
419-
# content of conftest.py
420-
421-
422-
def pytest_configure(config):
423-
your_module._called_from_test = True
412+
import sys
424413
425-
and then check for the ``your_module._called_from_test`` flag:
426414
427-
.. code-block:: python
415+
_called_from_test_by_pytest = "pytest" in sys.modules
428416
429-
if your_module._called_from_test:
430-
# called from within a test run
417+
if _called_from_test_by_pytest:
418+
# called from within a test run by pytest
431419
...
432420
else:
433421
# called "normally"

0 commit comments

Comments
 (0)