Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Making module content available in parent module

Reply
Thread Tools

Making module content available in parent module

 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      11-23-2010
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?

--
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

 
Reply With Quote
 
 
 
 
Gregor Horvath
Guest
Posts: n/a
 
      11-23-2010
Hi,

On Tue, 23 Nov 2010 11:36:05 +0100
Ulrich Eckhardt <(E-Mail Removed)> wrote:

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

--
Gregor
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-23-2010
On Tue, 23 Nov 2010 11:36:05 +0100, Ulrich Eckhardt wrote:


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


--
Steven
 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      11-23-2010
Steven D'Aprano wrote:
> On Tue, 23 Nov 2010 11:36:05 +0100, Ulrich Eckhardt wrote:
>> 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.


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

--
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: Only Content controls are allowed directly in a content page that contains Content controls. hazz ASP .Net 6 06-09-2010 01:54 PM
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
Page load of the parent page called twice when a modal dialog is opened from a button click of the user control on the parent page Samy ASP .Net 2 08-15-2005 03:30 PM
Parent - Child back to parent javascript thingybob mark ASP .Net 1 03-10-2005 02:20 PM
What does sender.Parent.Parent.Cells() reference? Frustrating... Roy ASP .Net 2 02-11-2005 09:00 PM



Advertisments