Update docs: Documented a better way to detect if a code is running from within a pytest run
This commit is contained in:
parent
2e1dfb0c16
commit
0fdbb5f904
|
@ -409,25 +409,13 @@ running from a test you can do something like this:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
# content of your_module.py
|
import sys
|
||||||
|
|
||||||
|
|
||||||
_called_from_test = False
|
_called_from_test_by_pytest = "pytest" in sys.modules
|
||||||
|
|
||||||
.. code-block:: python
|
if _called_from_test_by_pytest:
|
||||||
|
# called from within a test run by pytest
|
||||||
# content of conftest.py
|
|
||||||
|
|
||||||
|
|
||||||
def pytest_configure(config):
|
|
||||||
your_module._called_from_test = True
|
|
||||||
|
|
||||||
and then check for the ``your_module._called_from_test`` flag:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
if your_module._called_from_test:
|
|
||||||
# called from within a test run
|
|
||||||
...
|
...
|
||||||
else:
|
else:
|
||||||
# called "normally"
|
# called "normally"
|
||||||
|
|
Loading…
Reference in New Issue