add an example for testing from an app if a test run is ongoing

--HG--
branch : trunk
This commit is contained in:
holger krekel 2010-10-17 00:10:22 +02:00
parent 170346654c
commit 80778eb3ae
2 changed files with 33 additions and 0 deletions

32
doc/example/misc.txt Normal file
View File

@ -0,0 +1,32 @@
Misc examples
====================================================
Detect if running from within a py.test run
--------------------------------------------------------------
Usually it is a good 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.

View File

@ -9,3 +9,4 @@ Usages and Examples
example/marking.txt
example/mysetup.txt
example/misc.txt