From 80778eb3aed5e6331866501f05e8e4023c0e10b0 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sun, 17 Oct 2010 00:10:22 +0200 Subject: [PATCH] add an example for testing from an app if a test run is ongoing --HG-- branch : trunk --- doc/example/misc.txt | 32 ++++++++++++++++++++++++++++++++ doc/examples.txt | 1 + 2 files changed, 33 insertions(+) create mode 100644 doc/example/misc.txt diff --git a/doc/example/misc.txt b/doc/example/misc.txt new file mode 100644 index 000000000..90866fa5d --- /dev/null +++ b/doc/example/misc.txt @@ -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. + diff --git a/doc/examples.txt b/doc/examples.txt index 9a7935a39..6a9aa7444 100644 --- a/doc/examples.txt +++ b/doc/examples.txt @@ -9,3 +9,4 @@ Usages and Examples example/marking.txt example/mysetup.txt + example/misc.txt