What are the naming conventions for PyTest test discovery?

Beginner

Answer

PyTest automatically discovers tests following these conventions:

  • Test files: test_*.py or *_test.py
  • Test functions: Functions starting with test_
  • Test classes: Classes starting with Test (no __init__ method)
  • Test methods: Methods in test classes starting with test_
# Valid test file: test_math.py
def test_addition():        # ✓ Discovered
    pass
class TestCalculator:       # ✓ Discovered
    def test_multiply(self): # ✓ Discovered
        pass
    def helper_method(self): # ✗ Not discovered
        pass