Parallel package hierarchies/directories

A

atleta

Hi,

I'd need a logical way to organize my unit tests into packages, but I
don't know how to do it in python. I'd like to separate my test
sources (modules) from the code. The problem is that it seems that
python does not allow a package to spread through multiple
directories. I'd like to have the following layout (which is the usual
way we do it e.g. in java):

+-project_root/
|
+-src/
| |
| +-soap
|
+-test/
|
+-soap

'soap' is a package under the source tree. I.e. I'd like my test
source tree to mirror the source tree and have package specific tests
in the same package as the code they excersize. However I found that
this is not possible, at least not the way I wanted, not without
moving files. I tried to place a script that would run all tests under
the 'test' directory. It added 'src' to the sys.path, but then it was
only able to import packages from test/soap and not src/soap.

Is there a way I can have python look for soap package modules in both
places? If not, then could someone tell me what is the usual way to
layout test code?

Thanks,
atleta
 
A

atleta

OK, I'm answering my own question, it might be of some help for
someone in the future: yes, it's possible to have parts of the same
package in multiple directories, it's just not enabled by default. To
make it work each such package should have the following code in their
__init__.py:

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

Actually pkgutil's documentation explains this quite clearly:
http://docs.python.org/library/pkgutil.html (It's easy if you know
where to look ;) )

Laszlo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top