Files
pytest2/doc/example/detectpytest.txt
holger krekel f7b4f70a16 make examples less nested for now
--HG--
branch : trunk
2010-10-27 17:36:27 +02:00

30 lines
861 B
Plaintext

Detect if running from within a py.test run
--------------------------------------------------------------
Usually it is a bad idea to make application code
behave differently if called from a test. But if you
absolutely must find out if your application code is
running from a test you can do something like this::
# content of conftest.py in your testing directory
def pytest_configure(config):
import sys
sys._called_from_test = True
def pytest_unconfigure(config):
del sys._called_from_test
and then check for the ``sys._called_from_test`` flag::
if hasattr(sys, '_called_from_test'):
# called from within a test run
else:
# called "normally"
accordingly in your application. It's also a good idea
to rather use your own application module rather than ``sys``
for handling flag.