6

When a python module has multiple subpackages, where should tests for features in those subpackages be placed?

I can see two ways it could be done:

  • Create a separate test folder in each subpackage and place its tests there.
  • Duplicate the package hierarchy in the top-level test folder, placing the tests for each subpackage in the corresponding folder.

However it's not clear which option should be preferred.

For a package foo arranged like this:

foo/
  __init__.py
  bar.py
  baz/
    __init__.py
    baz.py

Do I put the tests here?

foo/
  __init__.py
  bar.py
  baz/
    __init__.py
    baz.py
  test/
    __init__.py
    test_bar.py
    baz/
      __init__.py
      test_baz.py

Or here?

foo/
  __init__.py
  bar.py
  baz/
    __init__.py
    baz.py
    test/
      __init__.py
      test_baz.py
  test/
    __init__.py
    test_bar.py
AJMansfield
  • 4,039
  • 3
  • 29
  • 50

1 Answers1

1

There is a similar answer here:

https://stackoverflow.com/a/24266885/6529424

But it really comes down to preference/style or it depends on whatever framework you use to test your code.

eagle33322
  • 228
  • 2
  • 11