[TIP] Coverage does not cover script if py.test executes it from another directory
ned at nedbatchelder.com
Sat Sep 7 07:56:56 PDT 2013
On 9/6/13 8:35 AM, Christoph Buchner wrote:
> Ned Batchelder schrieb am 06.09.2013 04:36:
>> On 9/5/13 6:33 AM, Christoph Buchner wrote:
>>> Sure, see attachment. step-by-step instructions in Instructions.txt
>> Christoph, when I follow your instructions, I get the same output for
>> both commands. They both print "--Contents of arg_file.txt--", and both
>> say 0% coverage for my_script.py when the report is generated. I've
>> attached the complete output.
>> I'll look into why the script isn't measured (and why there's no debug
>> message that it isn't being measured), but it's disconcerting that our
>> results are different.
> this is indeed weird! I'll try this again on my other machine, but I'm pretty sure it showed the same behaviour.
> I haven't run my tests in a virtual environment, should I look into setting one up, too, and try again?
> I'm already curious what you find out about why the script is not measured, and no debug output about it either!
I've learned many things...
1) The difference in behavior for our two configurations is the
virtualenv: my_script.py has a shebang line of /usr/bin/python, which
forced it out of my virtualenv, onto a Python that was not configured
for coverage. I changed it to "/usr/bin/env python", which is always a
2) The test_in_tmpdir case then wasn't getting debug tracing because the
COVERAGE_PROCESS_START environment variable is set to ".coveragerc".
When your program is run in the tmp dir, that relative path name
references a file that does not exist (in the tmp dir). So you are
using coverage.py defaults instead of your own settings. This explains
why I didn't even get debug tracing for the subprocess in the tmp dir.
3) Because your program runs in the tmp dir, the coverage data file is
written to a data file in the tmp dir. That file will not be combined
or read later during the reporting phase. In fact, it probably no
longer exists, having been cleaned up with the rest of the tmp dir.
To fix your problem:
a) export COVERAGE_PROCESS_START=/full/path/to/.coveragerc
b) In your .coveragerc:
data_file = /full/path/to/.coverage
I hope you get the same (good) results I did.
More information about the testing-in-python