add an example for testing from an app if a test run is ongoing
--HG-- branch : trunk
This commit is contained in:
parent
170346654c
commit
80778eb3ae
|
@ -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.
|
||||
|
|
@ -9,3 +9,4 @@ Usages and Examples
|
|||
|
||||
example/marking.txt
|
||||
example/mysetup.txt
|
||||
example/misc.txt
|
||||
|
|
Loading…
Reference in New Issue