Testing
The code that you write in the bricks is just Python. That means you can use your favorite tools as you would with any mainstream Python project.
Testing Bricks
By default, tests are added when creating a new component or base with the poly create
command.
This is optional and can be turned off in the Workspace configuration.
The tests are added in a test
folder at the root of the workspace with the same kind of folder structure as the bricks.
Example
Creating a new parser
component. This will add a new brick to the components
folder.
Poetry
Hatch
PDM
Rye
uv
Maturin
# if not already activated a virtual environment
source .venv/bin/activate
poly create component --name parser
A corresponding unit test will also be created in the test
folder:
Add the proper assertions to your tests during development of the bricks.
Running tests
Running Pytest from the workspace root:
Poetry
Hatch
PDM
Rye
uv
Maturin
Running tests with pytest and the TDD Theme
This configuration ensures the entire namespace of your brick is included
when pytest
does it's test lookup. Without it, pytest
will raise errors
because of the default way it does module lookups. This occurs when using Polylith with the TDD theme.
Running tests for changed code
The Python tools for the Polylith Architecture doesn't (yet) have a specific test
command.
You can use poly diff
and your favorite test runner to only run the corresponding tests for changed code.
The diff
command has support for displaying the changed bricks by using --bricks
.
Append the --short
option for a scripting-friendly output.
You can use the output from the poly diff
command to run specific tests.
Storing a list of bricks in a bash variable:
Poetry
Hatch
PDM
Rye
uv
Maturin
# if not already activated a virtual environment
source .venv/bin/activate
changes="$(poly diff --bricks --short)"
To include bricks that use any of the changed bricks, you can add the --deps
option.
This will append any dependent bricks to the output.
By having tests in the same kind of structure as the bricks,
you can use the output from the poly diff
command to pick the tests to run.
You can also name the individual test functions to include brick names, or use decorators (such as pytest
markers).
Pytest
Transform the result of the poly diff
command into a Pytest keyword or marker expression.
(i.e. from hello,world,something to hello or world or something).
-k
is for running tests by keyword expressions.-m
is for running tests by marker expressions.
Poetry
Hatch
PDM
Rye
uv
Maturin
Manually testing and running services
The bases
and components
is plain Python,
and the code can be run as you would if it were a single project repository.
The bases are the entry points to your services and apps.
Example: running a FastAPI service locally
Start the service by specifying the namespace path to your entry point (i.e. the base
).
It is also possible to test out project-specific
entries that are defined in a pyproject.toml
as you would in a mainstream Poetry project.