How do you guys organize your tests in a monorepo?...
# general
a
How do you guys organize your tests in a monorepo? When we were in a multirepo setting we had the following folder structure for all projects:
Copy code
my-package/
    tests/
        integration/
        unit/
        some_shared_module.py
    my_package/
This leads to ambiguous imports in a monorepo setting, e.g.
from tests.some_shared_module import some_function
. It's not something we use often, as most things are better suited in
conftest.py
, but often enough that Pants is complaining very loudly. From what I can tell I can either remove the ambiguity by specifying dependencies explicitly, or I can move all tests to a top-level
tests
folder with sub-folders for each project. Really just keen to hear how others have dealt with this and if there are other methods I haven't thought of.
l
I'm sure you've thought of this, but is the
some_shared_module
really shared? If so, you could consolidate it in in one place and have it imported from there. I.e. you don't have to have a top-level
tests
folder but it sounds like you do want a top-level
test_utils
folder.
and then
from test_utils.some_shared_module import some_function
b
my work repo does top-level
tests
as you mention, Pants itself does
..._test.py
files adjacent to the real code (i.e.
foo.py
is a sibling of
foo_test.py
)
πŸ‘ 1
a
Fair point @late-advantage-75311. In this case it's more shared for the specific set of tests than for the repository as a whole, but I guess it's subjective if it then depends in conftest.py instead.
b
If it’s just scoped in to the directory, does a relative import work
from . import some_shared_module
? Maybe another option to toss on the pile to consider
πŸ‘ 1
h
I love having foo/bar/baz.py tested by foo/bar/baz_test.py
It makes it really easy to find the test for a piece of code
This is common in C++ and Rust even lets you have tests in the same file as the source under test!
JVM and Python traditionally did not go that route, because the tooling assumed that everything under a package root was to be compiled, packaged and distributed, with no fine-grained dependency management.
But... Pants provides that fine-grained dependency management!
So, why not?
πŸ‘ 1