Skip to content

Commit e2a0987

Browse files
authored
Update advice about _called_from_test. (#6168)
Update advice about _called_from_test.
2 parents 92d6a05 + bd68c2a commit e2a0987

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

doc/en/example/simple.rst

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,34 +302,31 @@ running from a test you can do something like this:
302302

303303
.. code-block:: python
304304
305-
# content of conftest.py
305+
# content of your_module.py
306306
307307
308-
def pytest_configure(config):
309-
import sys
308+
_called_from_test = False
310309
311-
sys._called_from_test = True
310+
.. code-block:: python
312311
312+
# content of conftest.py
313313
314-
def pytest_unconfigure(config):
315-
import sys
316314
317-
del sys._called_from_test
315+
def pytest_configure(config):
316+
your_module._called_from_test = True
318317
319-
and then check for the ``sys._called_from_test`` flag:
318+
and then check for the ``your_module._called_from_test`` flag:
320319

321320
.. code-block:: python
322321
323-
if hasattr(sys, "_called_from_test"):
322+
if your_module._called_from_test:
324323
# called from within a test run
325324
...
326325
else:
327326
# called "normally"
328327
...
329328
330-
accordingly in your application. It's also a good idea
331-
to use your own application module rather than ``sys``
332-
for handling flag.
329+
accordingly in your application.
333330

334331
Adding info to test report header
335332
--------------------------------------------------------------

0 commit comments

Comments
 (0)