[TIP] Where to put unit tests? (What to name subdirectory?)

Chris Lasher chris.lasher at gmail.com
Wed Dec 17 22:47:33 PST 2008

On Thu, Dec 18, 2008 at 12:52 AM, Chris Lasher <chris.lasher at gmail.com>wrote:

> On Wed, Nov 12, 2008 at 3:58 PM, Kumar McMillan <kumar.mcmillan at gmail.com>wrote:
>> 2. put all tests within your package.  So if your package is
>> foo/__init__.py you would have a directory like foo/tests/__init__.py,
>> foo/tests/test_utils.py, etc.  They will get deployed via python
>> setup.py install
> What's the best way to import the module whose fixtures you want to test
> using this model, where your test module is in the tests/ subdirectory
> (pkg/tests/foo_tests.py), and the module is in the root directory
> (pkg/foo.py)? How do you put the the modules on sys.path for the tests, and
> make it work any way (e.g., if you're using setup.py to call out your
> tests)? Anyone care to point to code that uses this layout so we can get an
> example of how this is used in practice?

Ah, perhaps I should have Googled it. I thought about it more and used
Google Code search until I stumbled upon the MoinMoin unit tests:

In there is a neat idiomatic snippet to ensure that the tests, which are in
the subdirectory "tests/" can always import the modules above them in the
root directory:

moinpath = os.path.join(os.path.dirname(sys.argv[0]), os.pardir)
sys.path.insert(0, os.path.abspath(moinpath))

That's pretty clever, and works a treat! I'm using this for my own projects.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.idyll.org/pipermail/testing-in-python/attachments/20081218/8dbc69cc/attachment.htm 

More information about the testing-in-python mailing list