Correct way to share code between tests of different components #263
-
Hi, We recently started using python-polylith to build out our new monorepo. It's going well, and as the number of components we create is increasing - we're coming across cases where it makes sense to share test code between components. E.g. fixtures, stubs and utilities. Say we have a component I just wanted to confirm whether this is the suggested way to achieve this, or whether there is an alternative approach we should be using? Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
We did something similar. To prevent test code from polluting components, we created a That way test code is reusable but separate from components so it doesn't slip into project artifacts (i.e. Docker images and pip packages). Side note: export PYTHONPATH="${PWD}/components:${PWD}/bases" We use VS Code for python development so we had to set the // enable polylith to work with VS Code
// enables references to start from 'components' and 'base' dirs instead of root
"python.analysis.extraPaths": ["components", "bases"],
"python.autoComplete.extraPaths": ["components", "bases"],
"terminal.integrated.env.linux": {
"PYTHONPATH": "${workspaceFolder}/components:${workspaceFolder}/bases"
},
"terminal.integrated.env.windows": {
"PYTHONPATH": "${workspaceFolder}\\components;${workspaceFolder}\\bases"
}, |
Beta Was this translation helpful? Give feedback.
-
You also can have test-specific code (helpers/utils) within the (example, using
Here, the # this is a unit test in the tests/components/<the-top-namespace>/<the-package-to-tests> folder
# importing the helper/common to be used by tests only
from test_helpers import test_data
def test_something():
data = test_data.helloworld()
.... For |
Beta Was this translation helpful? Give feedback.
-
Thank you for both of your replies - I'll give these suggestions a go! As we're currently using the TDD theme, it looks like I may need to configure Poetry slightly differently - but this looks like a great approach. |
Beta Was this translation helpful? Give feedback.
We did something similar. To prevent test code from polluting components, we created a
test_utils
component and put component-specific test code in submodules oftest_utils
. In your example, I think test code forfoo
andbar
would be located inbar.test_utils.foo
andbar.test_utils.bar
respectively.That way test code is reusable but separate from components so it doesn't slip into project artifacts (i.e. Docker images and pip packages).
Side note:
components
should not be a module. To importfixtures
, it should be:from bar.test.bar import fixtures
. To make that work, we had to addcomponents
andbases
to ourPYTHONPATH
. Like this:We us…