test_example.py example_test.py testexample.py example.py
Pytest discovers test files that match the pattern test_*.py or *_test.py. So test_example.py and example_test.py are discovered. testexample.py and example.py do not match these patterns.
def test_add(): pass def add_test(): pass def Test_add(): pass def testAdd(): pass
Pytest discovers test functions whose names match the default test_* pattern (using fnmatch), meaning they must start with test_ (lowercase 'test' followed by an underscore). Only test_add matches. add_test does not start with test_, Test_add starts with uppercase 'T', and testAdd lacks the underscore after 'test'.
def _test_subtract(): assert 5 - 3 == 2
Pytest discovers functions starting with 'test' but not those starting with an underscore. The underscore prefix makes pytest ignore the function.
class TestCalculator: def test_add(self): pass class CalculatorTest: def test_subtract(self): pass class testCalculator: def test_multiply(self): pass class Test_calculator: def test_divide(self): pass
Pytest discovers classes whose names start with 'Test' exactly (uppercase T). So TestCalculator and Test_calculator are discovered. CalculatorTest ends with 'Test' but does not start with it, so it is ignored. testCalculator starts with lowercase 't' so it is ignored.
In pytest.ini, the option python_files controls which file patterns pytest uses to discover test files. Setting python_files = check_*.py makes pytest look for files starting with 'check_'.