Where should I put my tests in my package?

Right now, I've got a project that started off looking sort of like this:
pitz/ pitz/ cmdline/ webapp/ entity/
tests/ test_cmdline.py test_entity.py test_webapp.py
In my setup.py file, I have this line
packages=find_packages(exclude=[''tests']),
so that the tests don't get installed when the code is installed.
This works OK for now, but I'm adding more folders under the original folders, like this:
pitz/ pitz/ cmdline/ webapp/ handlers/ __init__.py statichandler.py
entity/
tests/ test_cmdline.py test_entity.py test_webapp.py test_handlers.py test_statichandler.py
It is getting more difficult to figure out the links between test files and code files.
I have a few questions:
1. I would like to put lots of tests/ folders next to the code they test, rather than at the top level of the project. How do I make sure the tests folders don't get installed? 2. Should I worry about not installing tests? Should I install them? 3. Is there any pattern I should follow for organizing my tests within my project?
Thanks for the help.
Matt

On Wed, Nov 17, 2010 at 10:14:13AM -0500, W. Matthew Wilson wrote:
I have a few questions:
- I would like to put lots of tests/ folders next to the code they
test, rather than at the top level of the project. How do I make sure the tests folders don't get installed? 2. Should I worry about not installing tests? Should I install them?
There was a discussion on the testing-in-python list a while ago: http://lists.idyll.org/pipermail/testing-in-python/2010-July/003147.html
IIRC there was no consensus reached.
- Is there any pattern I should follow for organizing my tests
within my project?
I like to put the tests inside my packages and install them:
pitz/ src/ pitz/ cmdline/ tests/ webapp/ handlers/ tests/ tests/ entity/ tests/ tests/
I also like to have one test module for each code module.
Marius Gedminas

On Wed, Nov 17, 2010 at 11:26 AM, Marius Gedminas marius@pov.lt wrote:
- I would like to put lots of tests/ folders next to the code they
test, rather than at the top level of the project. How do I make sure the tests folders don't get installed?
- Is there any pattern I should follow for organizing my tests
within my project?
My approach is a bit different from what has been suggested. I create a separate tests directory which mirrors the structure of the packages I want to test, and install them in a (share) data directory. So, in setup.py I have
import os, os.path ... data_dir = 'share/pitz/' data_files = \ [(os.path.join(data_dir, root), [os.path.join(root, file_name) for file_name in files]) for root, dirs, files in os.walk('tests')] + other_data_files ...
and in MANIFEST.in I have
recursive-include tests *.py
so the directory structure would look like this:
pitz/ src/ pitz/ cmdline/ webapp/ handlers/ __init__.py statichandler.py entity/
tests/ pitz/ cmdline/ webapp/ handlers/ __init__.py test_statichandler.py entity/
setup.py MANIFEST.in
I like this approach because it cleanly separates the tests from the actual package code, the tests go into the data directory, rather than with the package itself, and I can easily decide whether to include or not the tests with the distribution.

On Wed, Nov 17, 2010 at 10:14 AM, W. Matthew Wilson matt@tplus1.com wrote:
- I would like to put lots of tests/ folders next to the code they
test, rather than at the top level of the project. How do I make sure the tests folders don't get installed?
I'm sure there's a way to prevent installation in this case, but I don't know what it is.
- Should I worry about not installing tests? Should I install them?
Unless you're targeting storage-constrained platform, why shouldn't you install them?
- Is there any pattern I should follow for organizing my tests
within my project?
There's a lot of variations here. Some people prefer your original arrangement, and others prefer embedding the tests within each package as you're describing for your new arrangement. (The later is widely used in the Zope community.)
Another approach, good if you want to avoid installation of the tests, is to use a mirror hierarchy for your tests:
pitz/ pitz/ cmdline/ webapp/ handlers/ __init__.py statichandler.py
entity/
tests/ commandline/ test_cmdline.py webapp/ test_webapp.py handlers/ test_handlers.py test_statichandler.py entity/ test_entity.py
I favor the Zopish approach these days.
-Fred
-- Fred L. Drake, Jr. <fdrake at acm.org> "A storm broke loose in my mind." --Albert Einstein
participants (4)
-
Ernesto Posse
-
Fred Drake
-
Marius Gedminas
-
W. Matthew Wilson