by
tundish
2012 October 28
Sunday evening

Python namespace packages are a good way of organising large projects.

If you're doing test-driven development, you'll want to run all the unit tests defined in every subpackage you have installed. That is, discovering all the packages under your namespace.

Nose can do this, but its command line options are a little complicated. So to help, you can provide your own test runner which configures nose in just the right way.

Here's how the devops package for your namespace (hwit in this example) could define a handy entry point to run the tests across the whole namespace.

This is the content of the file nosemain.py.

import nose
import pkg_resources

_path = pkg_resources.resource_filename("hwit.devops", "nosemain.py")
_parent = os.path.normpath(os.path.join(os.path.dirname(_path), ".."))


def run():
    args = sys.argv + [opt for opt in
   ("--exe",
    "--all-modules",
    "--traverse-namespace",
    "--verbosity=2",
    "--with-id",
    "--with-xunit",
    "--where=%s" % _parent)
    if opt not in sys.argv
    ]
    nose.run(argv=args)

The output ends up in a nosetests.xml file (well-known to CI systems like Jenkins).

We know because we do. We do because we can.