Registry / testing / pytestarch

pytestarch

JSON →
library4.0.1pypypi✓ verified 85d ago

pytestarch is a Python test framework that enables static analysis of software architecture by defining and enforcing rules based on import dependencies between modules. It integrates seamlessly with pytest to ensure architectural constraints are met. The current version is 4.0.1, with major releases occurring periodically to introduce new features and adapt to Python version changes.

pip install pytestarch
INSTALL
IMPORT
SIG · PYTESTARCH
P
pytestarch
testingpythonv4.0.1
Install
2.6s avg
Import
Disk
33MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 32.7MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.6s · import 0.000s · 33MB
33MB installed
● package 33MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Rule
from pytestarch import Rule
from pytestarch import architectural_rule
LayerRule
from pytestarch import LayerRule
EvaluableArchitecture
from pytestarch import EvaluableArchitecture

This quickstart demonstrates how to define and verify architectural layer dependencies using `pytestarch`. It assumes a standard Python project layout with a `src/` directory containing your modules and a `tests/` directory for your `pytestarch` test files. The `PYTEST_ARCH_ROOT` environment variable is crucial for module discovery.

# Assuming a project structure like: # project_root/ # ├── src/ # │ ├── __init__.py # │ ├── domain/ # │ │ ├── __init__.py # │ │ └── core.py # │ ├── application/ # │ │ ├── __init__.py # │ │ └── service.py # e.g., 'from src.domain import core' # │ └── infrastructure/ # │ ├── __init__.py # │ └── db.py # e.g., 'from src.domain import core' # └── tests/ # └── test_architecture.py # The PYTEST_ARCH_ROOT environment variable MUST be set to the path of your # source code root (e.g., './src') for pytestarch to discover modules correctly. # In tests/test_architecture.py from pytestarch import architectural_rule from pytestarch.patterns import Layer, get_layers def test_layer_dependencies(): # Define the layers in your application. # The first argument to get_layers is the base directory to scan for modules. # The second argument is a list of Layer objects. # Each Layer needs a unique name and a module path regex (e.g., 'src.domain'). layers = get_layers( "src", # Base directory relative to where pytest is run, or an absolute path [ Layer("domain", "src.domain"), Layer("application", "src.application"), Layer("infrastructure", "src.infrastructure"), ] ) # Define architectural rules: # 1. All modules discovered must belong to one of the defined layers. # 2. Layers should not import modules from other layers by default. # 3. Specifically, 'application' and 'infrastructure' layers are allowed # to import *only* from the 'domain' layer. architectural_rule.modules_belong_to_layers(layers) \ .should_not_import_modules_of_other_layers() \ .may_only_import_modules_of_layers("domain") \ .in_layers("application", "infrastructure") \ .check() # To run this test, navigate to your project_root and execute in your terminal: # PYTEST_ARCH_ROOT=./src pytest tests/test_architecture.py
Debug
Known issues
breakingThe mechanism of exporting library modules changed in v4.0.0. This could break advanced integrations or direct internal module imports that relied on previous export paths.
fix
Review the official documentation for updated module export paths and adapt any direct imports or custom extensions as necessary.
affects: >=4.0.0
breakingSupport for Python 3.8 was officially dropped in version 3.0.0. Attempting to use pytestarch v3.0.0 or later with Python 3.8 will result in compatibility errors.
fix
Upgrade your Python environment to version 3.9 or newer to ensure compatibility.
affects: >=3.0.0
gotchapytestarch relies heavily on the `PYTEST_ARCH_ROOT` environment variable to correctly identify the root of your source code. If this is not set or set incorrectly, module discovery and rule evaluation will fail, often leading to `ModuleNotFoundError` or `KeyError`.
fix
Ensure `PYTEST_ARCH_ROOT` is set (e.g., `PYTEST_ARCH_ROOT=./src pytest`) to the absolute or correct relative path of your main source directory. Additionally, verify that the `root_path` argument passed to `get_layers` aligns with how your modules are imported (e.g., 'src' if your imports are `from src.module ...`).
affects: All versions
gotchaFailure to include all relevant top-level modules or submodules in your `Layer` definitions, especially when using regular expressions, can lead to `KeyError` or modules being incorrectly categorized as 'not part of a layer'. This was specifically addressed for certain `KeyError` scenarios in v4.0.1.
fix
Ensure your `Layer` definitions comprehensively cover all expected modules. If encountering `KeyError`, consider upgrading to v4.0.1+ and, if the issue persists, use more explicit module paths rather than overly broad regexes if necessary.
affects: v4.0.0
Upgrade
Version history
4.0.1latest on PyPI · released Aug 8, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
14
Resources
pytestarch — pip install pytestarch · libregistry