# How to Run Pytest Tests with Coverage for ADR Sensor Development

> Learn to run pytest tests with coverage for ADR Sensor development. Execute your test suite effectively and get detailed coverage reports for robust sensor development.

- Repository: [Uber Open Source/ADR](https://github.com/uber/ADR)
- Tags: how-to-guide
- Published: 2026-08-07

---

**Use `pytest tests/ -v --cov=adr_sensor` from the `Sensor/` directory to execute the test suite with coverage reporting, or run `uv run pytest -q` for a streamlined CI-style output.**

The ADR repository from Uber provides a complete testing setup for its sensor component. Learning how to run pytest tests with coverage for ADR Sensor development ensures your contributions meet quality standards and maintain reproducibility across environments.

## Prerequisites: Development Dependencies

The sensor package declares its testing stack in [`Sensor/pyproject.toml`](https://github.com/uber/ADR/blob/main/Sensor/pyproject.toml). The `[project.optional-dependencies]` section pins **pytest** ≥ 7.0.0 and **pytest-cov** ≥ 4.1.0 as `dev` extras【/cache/repos/github.com/uber/ADR/main/Sensor/pyproject.toml#L31-L35】.

Install these dependencies using the repository's **uv** package manager:

```bash
cd Sensor
uv sync --dev

```

This command reads the `uv.lock` file to reproduce the exact environment, including **ruff** for linting alongside the testing tools.

## Running Tests: Two Entry Points

### Direct Pytest Invocation

Execute tests from the `Sensor/` directory with standard pytest commands:

```bash

# Detailed output for debugging

pytest tests/ -v

# Quiet mode for CI pipelines

pytest tests/ -q

```

The default test location is configured in [`pyproject.toml`](https://github.com/uber/ADR/blob/main/pyproject.toml) under `tool.pytest.ini_options`, so `pytest` discovers tests in the `tests/` folder automatically.

### UV Wrapper Command

For reproducible environments without manual activation:

```bash
uv run pytest -q

```

This approach ensures the locked dependency versions are always used, matching the workflow described in the project's reproducibility documentation【/cache/repos/github.com/uber/ADR/main/docs/REPRODUCIBILITY.md#L47-L52】.

## Measuring Code Coverage

Add **pytest-cov** flags to any test command to measure line coverage for the `adr_sensor` package source code located at `Sensor/adr_sensor/`.

### Console Coverage Report

```bash
pytest tests/ -v --cov=adr_sensor

```

Outputs coverage percentages directly in your terminal after test completion.

### HTML Coverage Report

```bash
pytest tests/ -v --cov=adr_sensor --cov-report=html

```

Generates a browsable report at [`htmlcov/index.html`](https://github.com/uber/ADR/blob/main/htmlcov/index.html). View it with:

```bash
open htmlcov/index.html      # macOS

xdg-open htmlcov/index.html  # Linux

```

The HTML report highlights uncovered lines in modules like [`adr_sensor/observer.py`](https://github.com/uber/ADR/blob/main/adr_sensor/observer.py), helping you identify gaps in test coverage for the core orchestrator class.

## Complete Development Workflow

Follow this sequence when contributing to ADR Sensor:

1. **Sync environment**

   ```bash
   cd Sensor
   uv sync --dev
   ```

2. **Run tests during development**

   ```bash
   uv run pytest -q
   ```

3. **Validate with coverage before submitting**

   ```bash
   pytest tests/ -v --cov=adr_sensor --cov-report=html
   ```

4. **Review coverage gaps** in [`htmlcov/index.html`](https://github.com/uber/ADR/blob/main/htmlcov/index.html) and add tests for uncovered logic in `Sensor/tests/` modules like [`test_utils.py`](https://github.com/uber/ADR/blob/main/test_utils.py) or [`test_schemas.py`](https://github.com/uber/ADR/blob/main/test_schemas.py).

These commands match the official instructions in the Sensor README【/cache/repos/github.com/uber/ADR/main/Sensor/README.md#L227-L230】.

## Key Configuration Files

| File | Purpose |
|------|---------|
| [`Sensor/pyproject.toml`](https://github.com/uber/ADR/blob/main/Sensor/pyproject.toml) | Declares `dev` dependencies and `tool.pytest.ini_options` |
| `Sensor/uv.lock` | Pins exact versions of pytest, pytest-cov, and other tools |
| `Sensor/tests/` | Unit tests for parsers, schemas, and observer logic |
| [`docs/REPRODUCIBILITY.md`](https://github.com/uber/ADR/blob/main/docs/REPRODUCIBILITY.md) | CI-focused test commands for automated runs |

## Summary

- **Install**: `uv sync --dev` in `Sensor/` directory pulls pytest and pytest-cov
- **Run tests**: `pytest tests/ -v` or `uv run pytest -q`
- **Add coverage**: Append `--cov=adr_sensor` to measure package coverage
- **Generate reports**: Use `--cov-report=html` for detailed HTML output
- **Verify locally**: Check [`htmlcov/index.html`](https://github.com/uber/ADR/blob/main/htmlcov/index.html) before submitting changes

## Frequently Asked Questions

### What Python versions does ADR Sensor support for testing?

The [`pyproject.toml`](https://github.com/uber/ADR/blob/main/pyproject.toml) specifies Python version constraints that pytest inherits. Check the `requires-python` field in [`Sensor/pyproject.toml`](https://github.com/uber/ADR/blob/main/Sensor/pyproject.toml) for the supported range. The `uv.lock` file ensures tested versions are reproducible across machines.

### Why does coverage report zero percent for some modules?

Files in `adr_sensor/` that are never imported during test execution show zero coverage. Expand your test suite in `Sensor/tests/` to exercise modules like [`observer.py`](https://github.com/uber/ADR/blob/main/observer.py) or add [`__init__.py`](https://github.com/uber/ADR/blob/main/__init__.py) coverage by importing package-level constants in test files.

### Can I run a single test file instead of the full suite?

Yes. Specify the path directly: `pytest tests/test_schemas.py -v --cov=adr_sensor`. Pytest's test discovery still applies coverage measurement only to the executed code paths.

### How do I integrate this with GitHub Actions CI?

The same commands work in CI. Use `uv run pytest -q --cov=adr_sensor` for concise logs, and add `--cov-report=xml` to generate Cobertura-compatible output for coverage tracking services. The repository's reproducibility guide documents this pattern for automated runs without external API dependencies【/cache/repos/github.com/uber/ADR/main/docs/REPRODUCIBILITY.md#L47-L52】.