Pytest automatically discovers test files that start with test_ or end with _test.py. This naming helps pytest find and run your tests without extra configuration.
Click to reveal answer
beginner
What is the recommended naming pattern for test functions in pytest?
Test functions should start with test_. For example, def test_addition():. This allows pytest to recognize and run them as tests.
Click to reveal answer
beginner
Can you name a test file calculator.py and expect pytest to run tests inside it automatically?
No. Pytest will not discover tests in files without the test_ prefix or _test.py suffix. The file should be named like test_calculator.py.
Click to reveal answer
beginner
What happens if a test function does not start with test_ in pytest?
Pytest will ignore the function because it does not match the naming pattern for test functions. Only functions starting with test_ are run as tests.
Click to reveal answer
beginner
Why is consistent naming important in test files and functions?
Consistent naming helps pytest find tests automatically, keeps your project organized, and makes it easier for others to understand and run your tests.
Click to reveal answer
Which of these is a valid pytest test file name?
Atest_math.py
Bmath_tested.py
Cmath.py
Dtesting_math.py
✗ Incorrect
Pytest recognizes files starting with 'test_' or ending with '_test.py'. Only 'test_math.py' fits this pattern.
How should you name a test function for pytest to recognize it?
Atest_addition()
Baddition_test()
Ccheck_addition()
DAddTest()
✗ Incorrect
Test functions must start with 'test_' for pytest to discover and run them.
If a test file is named calculator.py, what will pytest do?
ARun all tests inside it
BIgnore it during test discovery
CRun tests only if specified manually
DRename it automatically
✗ Incorrect
Pytest ignores files without 'test_' prefix or '_test.py' suffix during automatic test discovery.
What prefix should test functions have in pytest?
Acheck_
Bverify_
Cassert_
Dtest_
✗ Incorrect
The 'test_' prefix is required for pytest to recognize functions as tests.
Why is it important to follow naming conventions in pytest?
ATo reduce code size
BTo make tests run faster
CTo allow pytest to find and run tests automatically
DTo avoid writing assertions
✗ Incorrect
Naming conventions help pytest discover tests without extra setup.
Explain the naming conventions for test files and test functions in pytest and why they matter.
Think about how pytest finds tests without manual configuration.
You got /4 concepts.
What happens if you name a test function without the 'test_' prefix in pytest?
Consider how pytest identifies test functions.
You got /3 concepts.
Practice
(1/5)
1. Which of the following is a correct name for a pytest test file?
easy
A. test_example.py
B. example_tested.py
C. example.py
D. mytestfile.py
Solution
Step 1: Understand pytest file naming rules
Pytest requires test files to start with test_ or end with _test.py.
Step 2: Check each option against the rule
test_example.py starts with test_, so it is correct. Options A, C, and D do not follow the naming convention.
Final Answer:
test_example.py -> Option A
Quick Check:
File starts with 'test_' = B [OK]
Hint: Test files start with 'test_' or end with '_test.py' [OK]
Common Mistakes:
Naming file without 'test_' prefix or '_test.py' suffix
Using generic file names without test indication
Confusing test function naming with file naming
2. Which of the following is the correct way to name a pytest test function?
easy
A. login_test()
B. check_login()
C. test_login()
D. TestLogin()
Solution
Step 1: Recall pytest function naming rule
Pytest requires test functions to start with test_ to be discovered automatically.
Step 2: Evaluate each function name
Only test_login() starts with test_. Others do not follow the rule.
Final Answer:
test_login() -> Option C
Quick Check:
Function starts with 'test_' = A [OK]
Hint: Test functions must start with 'test_' [OK]
Common Mistakes:
Omitting 'test_' prefix in function names
Using camel case instead of snake case
Confusing test file and function naming rules
3. Given the following test file named 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?
medium
A. Run the test function and fail
B. Run the test function and pass
C. Skip the test function
D. Raise a syntax error
Solution
Step 1: Check file and function naming
The file is named test_math.py and the function is named test_add, both following pytest conventions.
Step 2: Analyze test function behavior
The test asserts add(2, 3) == 5, which is true, so the test will pass.
Final Answer:
Run the test function and pass -> Option B
Quick Check:
Correct naming + passing assertion = A [OK]
Hint: Correct names run tests; passing asserts pass tests [OK]
Common Mistakes:
Misnaming test functions so pytest skips them
Assuming test fails without checking assertion
Confusing syntax errors with test failures
4. You have a test file named math_test.py with a function named add_test(). When running pytest, no tests are found. What is the most likely cause?
medium
A. The function name does not start with 'test_'
B. The file extension is incorrect
C. The file name does not start with 'test_'
D. The test function has no assert statement
Solution
Step 1: Check file naming
The file math_test.py ends with _test.py, which is valid for pytest discovery.
Step 2: Check function naming
The function is named add_test(), which does not start with test_. Pytest ignores such functions.
Final Answer:
The function name does not start with 'test_' -> Option A
Quick Check:
Function must start with 'test_' = C [OK]
Hint: Function names must start with 'test_' to be found [OK]
Common Mistakes:
Assuming file name alone is enough
Naming functions with '_test' suffix instead of prefix
Forgetting to add assert statements
5. You want pytest to automatically discover and run tests in your project. Which combination of file and function names will ensure this?