Making module content available in parent module

U

Ulrich Eckhardt

Hi!

Note up front: I'm using Python2.6 still, I guess with 2.7 test discovery, I
could get better results easier, right?

Now, my problem is I have a directory containing test scripts which I all
want to run. I used to run them individually and manually, but want to
avoid this overhead in the future.

tests/
foo.py # defines TestFoo1 and TestFoo2
bar.py # defines TestBar1 and TestBar2

What I would like to do now is this:

from tests import *
unittest.main()

In other words, import all test files and run them. This does import them,
but it turns out that I end up with modules foo and bar, and the unittests
inside those are not found.

Am I approaching the test loading the wrong way?


Cheers!

Uli


PS: I've been trying a few things here, and stumbled across another thing
that could provide a solution. I can "from tests import *", but then all
these modules will pollute my namespace. I can "import tests", but then
neither of the submodules will be in "tests". I tried "import tests.*", but
that doesn't work. Is there no way to import a whole package but with its
namespace?
 
G

Gregor Horvath

Hi,

Now, my problem is I have a directory containing test scripts which I
all want to run. I used to run them individually and manually, but
want to avoid this overhead in the future.

tests/
foo.py # defines TestFoo1 and TestFoo2
bar.py # defines TestBar1 and TestBar2

Nose does what you want:

http://packages.python.org/nose/
 
S

Steven D'Aprano

tests/
foo.py # defines TestFoo1 and TestFoo2
bar.py # defines TestBar1 and TestBar2

What I would like to do now is this:

from tests import *
unittest.main()

In other words, import all test files and run them. This does import
them, but it turns out that I end up with modules foo and bar, and the
unittests inside those are not found.

Given the directory structure you show, I find that hard to believe. You
should get an ImportError, as there is no module or package called
"tests".

But suppose you turn tests into a proper package:

tests/
__init__.py
foo.py
bar.py

You could have __init__.py include these lines:

from foo import *
from bar import *


Then later, when you do this:

from tests import *

it will pick up everything from foo and bar, and unittest.main() should
run those tests as well. I think.

Or you could just do:

for module in (foo, bar):
try:
unittest.main(module)
except SystemExit:
pass


PS: I've been trying a few things here, and stumbled across another
thing that could provide a solution. I can "from tests import *", but
then all these modules will pollute my namespace. I can "import tests",
but then neither of the submodules will be in "tests". I tried "import
tests.*", but that doesn't work. Is there no way to import a whole
package but with its namespace?

The package needs to know what submodules to make available. Put inside
__init__.py:


import foo
import bar



and then from outside the package, do this:

import tests

Now tests.foo and tests.bar will exist.
 
U

Ulrich Eckhardt

Steven said:
The package needs to know what submodules to make available. Put inside
__init__.py:


import foo
import bar

and then from outside the package, do this:

import tests

Now tests.foo and tests.bar will exist.

I've been reading up on packages, but I didn't find anything like that in
the documentation, all I found was the meaning of __all__. If I import the
modules explicitly there, there's no need to define __all__, unless there
are some I don't want to import there, right? Well, I'll try it and see. ;)

Steven, thank you for this explanation!

Uli
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top