📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero pytest Testing

pytest Testing

5 min read Quiz at the end
pytest auto-discovers and runs tests. Write test functions starting with test_ and use plain assert statements. Fixtures provide reusable setup code. Run pytest in the terminal to execute all tests.

Testing with pytest

pip install pytest

# test_math.py
def add(a, b): return a + b

def test_add():
    assert add(1, 2) == 3
    assert add(-1, 1) == 0

def test_add_strings():
    assert add("hello", " world") == "hello world"

# Run
# pytest test_math.py -v
Topic Quiz · 5 questions

Test your understanding before moving on

1. What naming convention does pytest require for test functions?
💡 pytest automatically discovers functions starting with test_.
2. Which assertion does pytest use?
💡 pytest uses plain Python assert statements — no special assertion methods.
3. How do you run pytest?
💡 Run pytest (or python -m pytest) in the terminal to discover and run tests.
4. What is a fixture in pytest?
💡 @pytest.fixture creates reusable setup code injected into test functions.
5. Which flag shows detailed test output?
💡 pytest -v shows verbose output with each test name and result.