Testing Guide
pyetm maintains high test coverage to ensure reliability. This guide explains our testing practices.
Running Tests
All Tests
With Coverage
Specific Tests
# Run one file
poetry run pytest tests/models/test_scenario.py
# Run one test
poetry run pytest tests/models/test_scenario.py::test_create_scenario
# Run tests matching pattern
poetry run pytest -k "scenario"
Writing Tests
Test Structure
import pytest
from pyetm import Scenario
def test_scenario_creation():
"""Test creating a basic scenario."""
scenario = Scenario.new(area_code="nl", end_year=2050)
assert scenario.area_code == "nl"
assert scenario.end_year == 2050
assert scenario.id is not None
Using Fixtures
import pytest
from pyetm import Client
@pytest.fixture
def client():
"""Fixture for authenticated client."""
return Client.from_env()
def test_with_client(client):
"""Test using the client fixture."""
assert client is not None
Mocking HTTP Requests
import pytest
import requests_mock
def test_api_call():
"""Test API call with mocked response."""
with requests_mock.Mocker() as m:
m.get("https://api.example.com/data", json={"result": 42})
response = requests.get("https://api.example.com/data")
assert response.json()["result"] == 42
Test Organization
tests/
├── conftest.py # Shared fixtures
├── models/ # Model tests
│ ├── test_scenario.py
│ ├── test_session.py
│ └── ...
├── services/ # Service tests
│ ├── test_scenario_runners.py
│ └── ...
└── test_cli.py # CLI tests
Best Practices
- Test one thing: Each test should verify one behavior
- Clear names: Use descriptive test names
- Arrange-Act-Assert: Structure tests clearly
- Use fixtures: Reuse common setup
- Mock external calls: Don't rely on external APIs
Next Steps
- Development Setup - Set up your environment
- Contributing Guide - Contribution workflow