Skip to content

Commit 4b4b1f3

Browse files
authored
test: migrate to pytest (#41)
1 parent ce7a021 commit 4b4b1f3

21 files changed

+1541
-1710
lines changed

CONTRIBUTING.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,22 @@ uv run ty check drift/ tests/ # Type check
3131
### Unit Tests
3232

3333
```bash
34-
uv run python -m unittest discover -s tests/unit -v
34+
uv run pytest tests/unit/ -v
3535

3636
# Run a specific test file
37-
uv run python -m unittest tests.unit.test_json_schema_helper -v
38-
uv run python -m unittest tests.unit.test_adapters -v
37+
uv run pytest tests/unit/test_json_schema_helper.py -v
38+
uv run pytest tests/unit/test_adapters.py -v
39+
40+
# Run a specific test class or function
41+
uv run pytest tests/unit/test_metrics.py::TestMetricsCollector -v
42+
uv run pytest tests/unit/test_metrics.py::TestMetricsCollector::test_record_spans_exported -v
3943
```
4044

4145
### Integration Tests
4246

4347
```bash
4448
# Flask/FastAPI integration tests
45-
timeout 30 uv run python -m unittest discover -s tests/integration -v
49+
timeout 30 uv run pytest tests/integration/ -v
4650
```
4751

4852
### E2E Tests

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ dev = [
5151
"python-jsonpath>=0.10",
5252
"ruff>=0.8.0",
5353
"ty>=0.0.1a7",
54-
"pytest>=8.0.0",
54+
"pytest>=8.0.0,<9.0.0",
55+
"pytest-mock>=3.15.0",
5556
]
5657

5758
[project.urls]

tests/conftest.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Pytest configuration and fixtures for Drift Python SDK tests."""
2+
3+
from __future__ import annotations
4+
5+
import os
6+
import tempfile
7+
from collections.abc import Generator
8+
from pathlib import Path
9+
from typing import TYPE_CHECKING
10+
11+
import pytest
12+
13+
if TYPE_CHECKING:
14+
from drift.core.metrics import MetricsCollector
15+
from drift.core.tracing.adapters import InMemorySpanAdapter
16+
17+
18+
@pytest.fixture
19+
def temp_dir() -> Generator[Path, None, None]:
20+
"""Create a temporary directory for test files."""
21+
with tempfile.TemporaryDirectory() as tmpdir:
22+
yield Path(tmpdir)
23+
24+
25+
@pytest.fixture
26+
def original_cwd() -> Generator[str, None, None]:
27+
"""Save and restore the current working directory."""
28+
cwd = os.getcwd()
29+
yield cwd
30+
os.chdir(cwd)
31+
32+
33+
@pytest.fixture
34+
def in_memory_adapter() -> InMemorySpanAdapter:
35+
"""Create a fresh InMemorySpanAdapter for testing."""
36+
from drift.core.tracing.adapters import InMemorySpanAdapter
37+
38+
return InMemorySpanAdapter()
39+
40+
41+
@pytest.fixture
42+
def metrics_collector() -> MetricsCollector:
43+
"""Create a fresh MetricsCollector for testing."""
44+
from drift.core.metrics import MetricsCollector
45+
46+
return MetricsCollector()

0 commit comments

Comments
 (0)