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

Antonio Cuni anto.cuni at gmail.com
Sun Oct 17 22:24:46 CEST 2010


Hi Holger, hi all

On 16/10/10 22:56, holger krekel wrote:
> 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 :)

sorry, I don't get it. Do you mean that I should describe my problem in more 
details? Ok, here is it.

I am developing a program using scrapy (to get data from web pages). Following 
the (bad?) habit of lot of frameworks, scrapy requires you to define some 
settings as global variables in a settings.py module.  In particular, the 
LOG_FILE variable determine where the log is written.

In my program, I would like the log file to be placed in a directory named 
after the date, e.g. '2010-10-17/myprogram.log'.  So, my settings.py contains 
something like:

LOGDIR = BASEDIR.join(str(datetime.date.today()))
LOGDIR.ensure(dir=True)
LOG_FILE = LOGDIR.join('myprogram.log')

The issue is simply that I don't want to create the directory when I run the 
test, as the log file is not needed (and even if it were, I'd want it to be in 
a separate tmpdir, not in the main directory).

All the other solutions I could think of require to change the code in scrapy 
itself, which I'd prefer not to do, of course.


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

+1 :-)


> 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?

I've not tried it yet, but should work and it's much better than my current 
approach :-). Thanks!

ciao,
Anto



More information about the Pytest-dev mailing list