How to Run Tests Using uv
If your project lists pytest as a dev dependency, run every test with a single command:
uv run pytestuv run syncs the virtual environment automatically, so the project and its dependencies are always installed before pytest starts.
If pytest is not in the project’s dependencies and you don’t want to add it, inject it at runtime:
uv run --with pytest pytestRun a Subset of Tests
Target a single file, a single function, or a keyword pattern:
uv run pytest tests/test_api.py
uv run pytest tests/test_api.py::test_login
uv run pytest -k "login or signup"The -k flag matches against test names, so -k "login or signup" runs every test whose name contains either word.
Run only tests tagged with a marker:
uv run pytest -m slowControl Output
Show each test name instead of a dot:
uv run pytest -vPrint print() output and logging to the terminal (pytest captures them by default):
uv run pytest -sShorten tracebacks to just the failing assertion:
uv run pytest --tb=shortStop Early on Failure
Stop after the first failure:
uv run pytest -xStop after n failures:
uv run pytest --maxfail=3Re-run Only Failed Tests
pytest records which tests passed and failed in .pytest_cache/. On the next run, --last-failed skips everything that passed:
uv run pytest --last-failedCombine with --fail-first to run the previously failed tests first, then the rest:
uv run pytest --fail-firstSet Persistent Defaults
Add flags to pyproject.toml so they apply on every run without typing them:
[tool.pytest.ini_options]
addopts = "-x --tb=short"Any flags passed on the command line are appended to addopts, so uv run pytest -v becomes -x --tb=short -v.
Learn More
- Setting up testing with pytest and uv walks through creating a project with tests from scratch
- How to test against multiple Python versions using uv covers
--pythonfor cross-version testing - How to run tests in parallel with pytest-xdist distributes tests across CPU cores
- How to fix common pytest errors with uv covers ModuleNotFoundError and other setup issues
- pytest reference