From bd68c2a3dca6e6aadd1f9a0e4810f53256d4f6a8 Mon Sep 17 00:00:00 2001 From: Michael Shields Date: Tue, 12 Nov 2019 02:02:47 +0000 Subject: [PATCH] Update advice about _called_from_test. Instead of giving an example of using sys and then, at the end, advising not to use sys, just give a correct example. This is especially helpful since mypy 0.740 has started (correctly) complaining about sys._called_from_pytest not being present. --- doc/en/example/simple.rst | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/doc/en/example/simple.rst b/doc/en/example/simple.rst index a7cd06d31..05ccbc9b2 100644 --- a/doc/en/example/simple.rst +++ b/doc/en/example/simple.rst @@ -300,36 +300,33 @@ behave differently if called from a test. But if you absolutely must find out if your application code is running from a test you can do something like this: +.. code-block:: python + + # content of your_module.py + + + _called_from_test = False + .. code-block:: python # content of conftest.py def pytest_configure(config): - import sys + your_module._called_from_test = True - sys._called_from_test = True - - - def pytest_unconfigure(config): - import sys - - del sys._called_from_test - -and then check for the ``sys._called_from_test`` flag: +and then check for the ``your_module._called_from_test`` flag: .. code-block:: python - if hasattr(sys, "_called_from_test"): + if your_module._called_from_test: # called from within a test run ... else: # called "normally" ... -accordingly in your application. It's also a good idea -to use your own application module rather than ``sys`` -for handling flag. +accordingly in your application. Adding info to test report header --------------------------------------------------------------