Good naming helps pytest find and run your tests automatically. It also makes your tests easy to understand.
Test file and function naming conventions in PyTest
Test file names: start with 'test_' or end with '_test.py' Test function names: start with 'test_'
Pytest automatically discovers files and functions following these patterns.
Using these conventions avoids the need to manually specify test locations.
test_math_operations.py def test_addition(): assert 1 + 1 == 2
calculator_test.py def test_subtraction(): assert 5 - 3 == 2
test_utils.py def test_helper_function(): assert helper() == 'done'
This test file named 'test_math.py' contains a test function 'test_add' that pytest will find and run automatically.
def add(a, b): return a + b # test_math.py def test_add(): assert add(2, 3) == 5 assert add(-1, 1) == 0 assert add(0, 0) == 0
Always start test function names with 'test_' so pytest can detect them.
Test files should be named starting with 'test_' or ending with '_test.py' for automatic discovery.
Keep names descriptive but simple to help others understand what is tested.
Test files must start with 'test_' or end with '_test.py'.
Test functions must start with 'test_'.
Following these rules lets pytest find and run your tests automatically.
Practice
Solution
Step 1: Understand pytest file naming rules
Pytest requires test files to start withtest_or end with_test.py.Step 2: Check each option against the rule
test_example.py starts withtest_, so it is correct. Options A, C, and D do not follow the naming convention.Final Answer:
test_example.py -> Option AQuick Check:
File starts with 'test_' = B [OK]
- Naming file without 'test_' prefix or '_test.py' suffix
- Using generic file names without test indication
- Confusing test function naming with file naming
Solution
Step 1: Recall pytest function naming rule
Pytest requires test functions to start withtest_to be discovered automatically.Step 2: Evaluate each function name
Only test_login() starts withtest_. Others do not follow the rule.Final Answer:
test_login() -> Option CQuick Check:
Function starts with 'test_' = A [OK]
- Omitting 'test_' prefix in function names
- Using camel case instead of snake case
- Confusing test file and function naming rules
test_math.py with this code:def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5
What will pytest do when run on this file?Solution
Step 1: Check file and function naming
The file is namedtest_math.pyand the function is namedtest_add, both following pytest conventions.Step 2: Analyze test function behavior
The test assertsadd(2, 3) == 5, which is true, so the test will pass.Final Answer:
Run the test function and pass -> Option BQuick Check:
Correct naming + passing assertion = A [OK]
- Misnaming test functions so pytest skips them
- Assuming test fails without checking assertion
- Confusing syntax errors with test failures
math_test.py with a function named add_test(). When running pytest, no tests are found. What is the most likely cause?Solution
Step 1: Check file naming
The filemath_test.pyends with_test.py, which is valid for pytest discovery.Step 2: Check function naming
The function is namedadd_test(), which does not start withtest_. Pytest ignores such functions.Final Answer:
The function name does not start with 'test_' -> Option AQuick Check:
Function must start with 'test_' = C [OK]
- Assuming file name alone is enough
- Naming functions with '_test' suffix instead of prefix
- Forgetting to add assert statements
- File:
example_test.py - Functions:
test_case1(),case2_test(),test_case3()
Solution
Step 1: Confirm file naming
The fileexample_test.pyends with_test.py, so pytest will discover it.Step 2: Check function naming rules
Pytest runs functions starting withtest_. Sotest_case1()andtest_case3()will run.case2_test()will be ignored.Final Answer:
Only test_case1() and test_case3() -> Option DQuick Check:
Functions start with 'test_' run = D [OK]
- Thinking '_test' suffix in function name triggers test
- Assuming all functions in test files run automatically
- Confusing file and function naming rules
Ready to prove it?
Pass the test and this pattern moves unknown
