Document using PYTEST_VERSION to detect if a code is running inside pytest (#12153)
Related to #9502
This commit is contained in:
parent
2e1dfb0c16
commit
2ede8778d0
|
@ -0,0 +1 @@
|
||||||
|
Documented using :envvar:`PYTEST_VERSION` to detect if code is running from within a pytest run.
|
|
@ -405,35 +405,20 @@ Detect if running from within a pytest run
|
||||||
Usually it is a bad idea to make application code
|
Usually it is a bad idea to make application code
|
||||||
behave differently if called from a test. But if you
|
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 this:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
# content of your_module.py
|
import os
|
||||||
|
|
||||||
|
|
||||||
_called_from_test = False
|
if os.environ.get("PYTEST_VERSION") is not None:
|
||||||
|
# Things you want to to do if your code is called by pytest.
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# 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"
|
# Things you want to to do if your code is not called by pytest.
|
||||||
...
|
...
|
||||||
|
|
||||||
accordingly in your application.
|
|
||||||
|
|
||||||
Adding info to test report header
|
Adding info to test report header
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue