Making module content available in parent module

Discussion in 'Python' started by Ulrich Eckhardt, Nov 23, 2010.

  1. 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
    Ulrich Eckhardt, Nov 23, 2010
    #1
    1. Advertising

  2. Hi,

    On Tue, 23 Nov 2010 11:36:05 +0100
    Ulrich Eckhardt <> 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
    Gregor Horvath, Nov 23, 2010
    #2
    1. Advertising

  3. 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
    Steven D'Aprano, Nov 23, 2010
    #3
  4. 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
    Ulrich Eckhardt, Nov 23, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy
    Replies:
    2
    Views:
    3,114
  2. Replies:
    5
    Views:
    1,868
    Andrew Thompson
    Jan 12, 2005
  3. hazz
    Replies:
    6
    Views:
    49,548
    SkyUCHC
    Jun 9, 2010
  4. Bostonasian
    Replies:
    1
    Views:
    816
    Joris Gillis
    Sep 18, 2005
  5. sharat
    Replies:
    2
    Views:
    228
    sharat
    Feb 17, 2012
Loading...

Share This Page