{"library":"pytestarch","title":"Pytest Arch","description":"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.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install pytestarch"],"cli":null},"imports":["from pytestarch import architectural_rule","from pytestarch import Rule","from pytestarch.patterns import Layer","from pytestarch.patterns import get_layers"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"# Assuming a project structure like:\n# project_root/\n# ├── src/\n# │   ├── __init__.py\n# │   ├── domain/\n# │   │   ├── __init__.py\n# │   │   └── core.py\n# │   ├── application/\n# │   │   ├── __init__.py\n# │   │   └── service.py # e.g., 'from src.domain import core'\n# │   └── infrastructure/\n# │       ├── __init__.py\n# │       └── db.py # e.g., 'from src.domain import core'\n# └── tests/\n#     └── test_architecture.py\n\n# The PYTEST_ARCH_ROOT environment variable MUST be set to the path of your\n# source code root (e.g., './src') for pytestarch to discover modules correctly.\n\n# In tests/test_architecture.py\nfrom pytestarch import architectural_rule\nfrom pytestarch.patterns import Layer, get_layers\n\ndef test_layer_dependencies():\n    # Define the layers in your application.\n    # The first argument to get_layers is the base directory to scan for modules.\n    # The second argument is a list of Layer objects.\n    # Each Layer needs a unique name and a module path regex (e.g., 'src.domain').\n    layers = get_layers(\n        \"src\", # Base directory relative to where pytest is run, or an absolute path\n        [\n            Layer(\"domain\", \"src.domain\"),\n            Layer(\"application\", \"src.application\"),\n            Layer(\"infrastructure\", \"src.infrastructure\"),\n        ]\n    )\n\n    # Define architectural rules:\n    # 1. All modules discovered must belong to one of the defined layers.\n    # 2. Layers should not import modules from other layers by default.\n    # 3. Specifically, 'application' and 'infrastructure' layers are allowed\n    #    to import *only* from the 'domain' layer.\n    architectural_rule.modules_belong_to_layers(layers) \\\n        .should_not_import_modules_of_other_layers() \\\n        .may_only_import_modules_of_layers(\"domain\") \\\n        .in_layers(\"application\", \"infrastructure\") \\\n        .check()\n\n# To run this test, navigate to your project_root and execute in your terminal:\n# PYTEST_ARCH_ROOT=./src pytest tests/test_architecture.py","lang":"python","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}