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

Kumar McMillan kumar.mcmillan at gmail.com
Wed Nov 12 12:58:10 PST 2008


On Wed, Nov 12, 2008 at 2:41 PM, Daryl Spitzer <daryl.spitzer at gmail.com> wrote:
> I apologize if this is a FAQ.  I didn't find the answer (or question)
> in a quick search.
>
> Is there a convention for where to put unit tests?

You are right to raise this question as there are many theories on
where to put tests but not much consensus (so it seems).

In order of popularity in what I [personally] have witnessed in open
source packages:

1. put all tests in a subdirectory named tests that is alongside the
setup.py file and is not within the package you are providing (i.e it
does not get deployed during python setup.py install)

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

3. put all tests alongside what they are testing.  i.e. you would end
up with a structure like:

foo/__init__.py
foo/frobulator.py
foo/frobulator_test.py
foo/session.py
foo/session_test.py
foo/bar/__init__.py
foo/bar_test.py


I prefer #2 myself because you can deploy the tests and thus your
users can run the tests or you can run tests on your production server
to verifiy deployment.  I have heard the argument in #3 that it makes
the project more manageable because whatever code you are working on
you know exactly where its tests are.  With this approach though I
think there are a lot of questions.  Like, where do you put non-unit
tests, those not tied to a specific module?  With #1 I hear the
argument that your tests are not deployed with your module and this is
a good thing but I forget why exactly.

Oh yeah, and there is a thread somewhere in the archives where we
debated #1 vs. #2, which is better.


-Kumar

>
> I favor putting them in a subdirectory named "UnitTests" (following
> Michael Foord's example in the Mock sources).  That way I can easily
> separate the unit tests from integration tests or system tests.
>
> But a colleague states that the python stdlib's tests are in a
> subdirectory called "tests".  Should I consider that a convention and
> stick with it (in spite of its disadvantages)?
>
> --
> Daryl
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>



More information about the testing-in-python mailing list