UV Pytest for Monorepos
Run pytest correctly in uv-managed monorepos with packages in packages/ directory.
Core Rules
ALWAYS follow these rules:
Use
uv run --directoryfor pytestuv run --directory packages/PACKAGE_NAME pytest [args]NEVER use bare
pytestcommand.Sync environment when modules are missing If you encounter import errors or "module not found":
uv sync --all-packages --all-groups --reinstallThen retry the pytest command.
NEVER use pip This is a uv-managed project. Use
uvcommands only, neverpip installor similar.
Common Scenarios
Run all tests for a specific package
uv run --directory packages/pitlane-core pytest
Run tests with verbosity
uv run --directory packages/pitlane-core pytest -v
Run specific test file
uv run --directory packages/pitlane-core pytest tests/test_session.py
Run specific test function
uv run --directory packages/pitlane-core pytest tests/test_session.py -k test_workspace_creation
Run with coverage
uv run --directory packages/pitlane-core pytest --cov
Using the Helper Script
A helper script is available that automatically syncs the environment before running tests:
./uv-pytest/scripts/run_pytest.sh PACKAGE_NAME [pytest-args]
Examples:
./uv-pytest/scripts/run_pytest.sh pitlane-core -v
./uv-pytest/scripts/run_pytest.sh pitlane-core tests/test_session.py -k test_function
The script ensures the environment is synced with --all-packages --all-groups --reinstall before running tests.
Troubleshooting
"Module not found" or import errors:
- Run:
uv sync --all-packages --all-groups --reinstall - Retry the pytest command
Tests not discovered:
- Verify you're using
--directory packages/PACKAGE_NAME - Check that the package name is correct
- Ensure you're running from the repository root
Wrong Python or dependencies:
- NEVER use pip to install packages
- Use
uv sync --all-packages --all-groups --reinstallto fix environment issues
Source: jshudzina/PitLane-AI — distributed by TomeVault.