[py-dev] Detect whether py.test is running

holger krekel holger at merlinux.eu
Sat Oct 16 22:56:43 CEST 2010


Hi Anto, all,

First of all, please keep asking "usage" questions or ask for examples
for practical / real life problems you have.  They help me filling
the example sections of the new py.test docs with useful material :)

On Sat, Oct 16, 2010 at 19:05 +0200, Antonio Cuni wrote:
> I need a way to distinguish whether the current module is being imported 
> because we are running the tests, of whether we are running the main program.
> 
> I know that the correct way would be to inject the information from the top, 
> but I'm using a framework which relies on some global state to store the 
> various options, and I have to deal with it.

Right, which is why i wouldn't like this to be totally convenient :)

> Anyway, is there any official/recommended way to do it?
> What I'm doing right now is this (py 1.3.1):
> 
> if py.test.config.__dict__ == {}:
>      # py.test is NOT running
>      ...
> else:
>      # py.test is running

uh, hacky :)  also will break with pytest-2.0 as the py.test.config
will not be there outside of test runs.

I drafted an example which should also work with py-1.3.1 as part of
the examples section of the upcoming pytest docs:

    http://codespeak.net/~hpk/pytest/example/misc.html

Works for you?

cheers,
holger



More information about the Pytest-dev mailing list