pytest is a popular Python testing framework with simple syntax and powerful features.
1# test_example.py2import pytest34def add(a, b):5 return a + b67# Simple test8def test_add():9 assert add(2, 3) == 510 assert add(-1, 1) == 01112# Parametrized tests13@pytest.mark.parametrize("a, b, expected", [14 (1, 2, 3),15 (-1, 1, 0),16 (0, 0, 0),17])18def test_add_param(a, b, expected):19 assert add(a, b) == expected2021# Fixtures22@pytest.fixture23def sample_user():24 return {"name": "Alice", "age": 25}2526def test_user_name(sample_user):27 assert sample_user["name"] == "Alice"2829# Exception testing30def test_division_by_zero():31 with pytest.raises(ZeroDivisionError):32 1 / 03334# Run: pytest test_example.py -v
Advantages: