[TIP] tox, 2to3 and py.test

Florian Bauer Florian.Bauer at diasemi.com
Tue Apr 3 01:57:38 PDT 2012

Hi Eduardo

> From: Eduardo Schettino [mailto:schettino72 at gmail.com] 
> On Mon, Apr 2, 2012 at 4:11 PM, Florian Bauer
> <Florian.Bauer at diasemi.com> wrote:
>> The py.test documentation recommends that test/ should not be a
>> package, and I understand why.
> I dont really understand why... and I could not find any
> reference in the docs on why this is recommended. Can someone
> please explain it?

Good that you ask, I think this point is a bit subtle.
The reason why you couldn't find anything in the docs is because I gave the wrong reference, it is actually in the tox docs at the bottom of the page here: http://tox.testrun.org/latest/example/pytest.html 

My project in its current form is a good example of the pitfalls of having a test package

Say this is my source code layout:


test_spam.py starts with
from mypackage.spam import eggs

if I now run py.test from my source directory, it would walk the file system and find the file mypackage/test/test_spam.py
Then it constructs the fully qualified name of this file, which is mypackage.test.test_spam , as it is part of a package, and imports it.
Likewise, mypackage.spam gets imported as well.
As long as I call py.test from the source directory, I know that I imported the files mypackage/spam.py and mypackage/test/test_spam.py

The fun begins in conjunction with tox and similar tools. Tox is supposed to run code installed in a private a virtualenv, say .tox/py26/lib/site-packages/mypackage or .tox/py32/lib/site-packages/mypackage
Which, in the given scenario it won't do, because mypackage/test/test_spam comes earlier on the module search path.

I showed a tox.ini in my first e-mail that works around the issue, but it took some time to figure out, why python3 was importing modules containing python 2 syntax ;)



Dialog Semiconductor GmbH
Neue Str. 95
D-73230 Kirchheim
Managing Directors: Dr. Jalal Bagherli, Jean-Michel Richard
Chairman of the Supervisory Board: Gregorio Reyes
Commercial register: Amtsgericht Stuttgart: HRB 231181
UST-ID-Nr. DE 811121668

Legal Disclaimer: This e-mail communication (and any attachment/s) is confidential and 
contains proprietary information, some or all of which may be legally privileged. It is 
intended solely for the use of the individual or entity to which it is addressed. Access 
to this email by anyone else is unauthorized. If you are not the intended recipient, any
disclosure, copying, distribution or any action taken or omitted to be taken in reliance
on it, is prohibited and may be unlawful.

Please consider the environment before printing this e-mail

More information about the testing-in-python mailing list