Write better tests with pytest.
Master testing patterns for Python applications.
Basic Test
def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
Fixtures
import pytest
@pytest.fixture
def client():
return TestClient(app)
def test_index(client):
response = client.get(“/”)
assert response.status_code == 200
Parametrization
@pytest.mark.parametrize(“input,expected”, [
(1, 1), (2, 2), (3, 6)
])
def test_factorial(input, expected):
assert factorial(input) == expected
Conclusion
pytest makes testing enjoyable!