File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ Documented using :envvar: `PYTEST_VERSION ` to detect if code is running from within a pytest run.
Original file line number Diff line number Diff line change @@ -405,35 +405,20 @@ Detect if running from within a pytest run
405405 Usually it is a bad idea to make application code
406406behave differently if called from a test. But if you
407407absolutely must find out if your application code is
408- running from a test you can do something like this:
408+ running from a test you can do this:
409409
410410.. code-block :: python
411411
412- # content of your_module.py
412+ import os
413413
414414
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
424-
425- and then check for the ``your_module._called_from_test `` flag:
426-
427- .. code-block :: python
428-
429- if your_module._called_from_test:
430- # called from within a test run
415+ if os.environ.get(" PYTEST_VERSION" ) is not None :
416+ # Things you want to to do if your code is called by pytest.
431417 ...
432418 else :
433- # called "normally"
419+ # Things you want to to do if your code is not called by pytest.
434420 ...
435421
436- accordingly in your application.
437422
438423 Adding info to test report header
439424--------------------------------------------------------------
You can’t perform that action at this time.
0 commit comments