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?
A. Test_xxx
B. test_xxx (starts with test_)
C. xxx_test at end
D. Any name
💡 pytest automatically discovers functions starting with test_.
2. Which assertion does pytest use?
A. assertEqual()
B. assert
C. expect()
D. assertTrue()
💡 pytest uses plain Python assert statements — no special assertion methods.
3. How do you run pytest?
A. python test.py
B. run pytest
C. pytest
D. python -m test
💡 Run pytest (or python -m pytest) in the terminal to discover and run tests.
4. What is a fixture in pytest?
A. A fake test
B. A reusable piece of test setup decorated with @pytest.fixture
C. A test runner
D. An assertion type
💡 @pytest.fixture creates reusable setup code injected into test functions.
5. Which flag shows detailed test output?
A. -d
B. -detail
C. -v
D. -verbose
💡 pytest -v shows verbose output with each test name and result.
Submit answers