Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put tests for subpackages in python?

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
like image 904
AJMansfield Avatar asked Nov 17 '22 01:11

AJMansfield


1 Answers

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.

like image 71
eagle33322 Avatar answered Dec 04 '22 09:12

eagle33322