Testing your patience

Marvelous maven Marcelo asks:

"Any recommendation/best practices to organize modules, and the respective test cases for them?"

Great question.

There are two schools of thought, for how to organize test code:

1) Embedded Tests

You have a source tree, with your Python code organized in different directories and sub-directories.

Modules and sub-modules.

At each level, in the exact same directory, you create test module. Containing the unit test cases.

So for a directory (module) tree like:

  1. polls/__init__.py
  2. polls/widgets.py
  3. polls/ab_split_testing/__init__.py
  4. quizzes/__init__.py
  5. quizzes/video.py
  6. quizzes/randomization/__init__.py
  7. quizzes/grading.py

You'll have test files named:

  1. test_polls.py
  2. polls/test_widgets.py
  3. polls/test_ab_split_testing.py
  4. test_quizzes.py
  5. quizzes/test_video.py
  6. quizzes/test_randomization.py
  7. quizzes/test_grading.py

There are other ways to do this, but this is the idea.

Before explaining the proz and conz of doing it this way, let me tell you about the other choice:

2) Parallel Hierarchy

Here, you'll have a top-level directory, holding the application source code. Named, often, "src/". Like this:

  1. src/polls/__init__.py
  2. src/polls/widgets.py
  3. src/polls/ab_split_testing/__init__.py
  4. src/quizzes/__init__.py
  5. src/quizzes/video.py
  6. src/quizzes/randomization/__init__.py
  7. src/quizzes/grading.py

And also at the top level, you'll have a "tests" directory. And it's where you stuff ALL your test code. With a tree of subdirectories that matches what's under src. Like this:

  1. tests/test_polls.py
  2. tests/polls/test_widgets.py
  3. tests/polls/test_ab_split_testing.py
  4. tests/test_quizzes.py
  5. tests/quizzes/test_video.py
  6. tests/quizzes/test_randomization.py
  7. tests/quizzes/test_grading.py

See how this works? These two approaches have their cons and pros:

1) With the "embedded test" approach:

2) With the "parallel hierarchy" approach:

I've worked on plenty of projects where I've used both approaches, and both will work fine. They just have different trade-offs, and you may prefer one or the other.

Just don't MIX them. For the love of Guido, pick one of these two, and stick with it.