Update advice about _called_from_test. (#6168)
Update advice about _called_from_test.
This commit is contained in:
commit
e2a0987156
|
@ -300,36 +300,33 @@ behave differently if called from a test. But if you
|
||||||
absolutely must find out if your application code is
|
absolutely must find out if your application code is
|
||||||
running from a test you can do something like this:
|
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
|
.. code-block:: python
|
||||||
|
|
||||||
# content of conftest.py
|
# content of conftest.py
|
||||||
|
|
||||||
|
|
||||||
def pytest_configure(config):
|
def pytest_configure(config):
|
||||||
import sys
|
your_module._called_from_test = True
|
||||||
|
|
||||||
sys._called_from_test = True
|
and then check for the ``your_module._called_from_test`` flag:
|
||||||
|
|
||||||
|
|
||||||
def pytest_unconfigure(config):
|
|
||||||
import sys
|
|
||||||
|
|
||||||
del sys._called_from_test
|
|
||||||
|
|
||||||
and then check for the ``sys._called_from_test`` flag:
|
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
if hasattr(sys, "_called_from_test"):
|
if your_module._called_from_test:
|
||||||
# called from within a test run
|
# called from within a test run
|
||||||
...
|
...
|
||||||
else:
|
else:
|
||||||
# called "normally"
|
# called "normally"
|
||||||
...
|
...
|
||||||
|
|
||||||
accordingly in your application. It's also a good idea
|
accordingly in your application.
|
||||||
to use your own application module rather than ``sys``
|
|
||||||
for handling flag.
|
|
||||||
|
|
||||||
Adding info to test report header
|
Adding info to test report header
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue