1.Test discovery and assertions
pytest finds tests automatically: files named test_*.py or *_test.py, functions named test_*, classes named Test*. Inside a test, a plain assert is enough — pytest rewrites it to show exactly what values did not match.
Key features
- pytest collects files matching test_*.py or *_test.py, without being told where they are
- A function is collected as a test if its name starts with test_
- A class is collected if its name starts with Test (and it has no __init__)
- A plain assert expression is enough — no assertEqual/assertTrue/assertIn methods needed
- On failure, assertion rewriting shows the actual values on both sides of the comparison
Discovery naming rules
- File matches test_*.py — discovered automatically, no registration needed
- Function matches test_* — collected and run as a test
- assert add(2, 2) == 5 — ordinary Python — no assertEqual needed
- Rewritten at collection time — failure shows: assert 4 == 5, + where 4 = add(2, 2)
Command
def test_something():
assert actual_value == expected_value
pytest.main() # or, from a shell: pytestpytest finds tests by name (test_*.py, test_* functions) — write plain assert statements, and pytest's rewriting shows exactly what did not match on failure.