Bird
Raised Fist0
PyTesttesting~20 mins

Test file and function naming conventions in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pytest Naming Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Identify valid pytest test file names
Which of the following file names will pytest automatically recognize as test files when running tests?
Atest_math.py, utils.py, helpers_test.py
Bmath_test.py, utils.py, test_helpers.py
Cmath.py, utils.py, helpers.py
Dtest_math.py, test_utils.py, math_test.py
Attempts:
2 left
💡 Hint
pytest looks for files starting with 'test_' or ending with '_test.py'.
🧠 Conceptual
intermediate
2:00remaining
Identify valid pytest test function names
Which function names will pytest recognize as test functions inside a test file?
Atest_addition, check_subtraction, test_multiplication
Badd_test, subtract_test, multiply_test
Ctest_addition, test_subtraction, test_multiplication
Daddition_test, test_subtraction, multiplication_test
Attempts:
2 left
💡 Hint
pytest looks for functions starting with 'test'.
❓ assertion
advanced
2:00remaining
Assertion failure due to incorrect test function name
Given the following test file named test_math.py with these functions, which function will NOT run during pytest execution?
PyTest
def test_add():
    assert 1 + 1 == 2

def check_subtract():
    assert 2 - 1 == 1

def test_multiply():
    assert 2 * 3 == 6
Acheck_subtract
BAll functions run
Ctest_multiply
Dtest_add
Attempts:
2 left
💡 Hint
pytest only runs functions starting with 'test'.
🔧 Debug
advanced
2:00remaining
Debug why pytest does not find tests in a file
You have a file named mathTests.py with functions named test_add and test_subtract. When running pytest, no tests are found. What is the most likely reason?
AFunction names do not start with 'test'
BFile name does not start with 'test_' or end with '_test.py'
CFunctions are missing assert statements
Dpytest is not installed
Attempts:
2 left
💡 Hint
pytest discovers test files by their names.
❓ framework
expert
3:00remaining
Best practice for organizing pytest test files and functions
Which of the following is the best practice for naming pytest test files and functions to ensure automatic discovery and clear organization?
AName test files starting with 'test_' and test functions starting with 'test_', use descriptive names for both
BName test files with any name, but test functions must start with 'test_'
CName test files ending with '_test.py' but test functions can have any name
DName test files and functions with any names as long as they are in a 'tests' folder
Attempts:
2 left
💡 Hint
pytest discovers tests based on file and function name patterns.

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

  1. Step 1: Understand pytest file naming rules

    Pytest requires test files to start with test_ or end with _test.py.
  2. 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.
  3. Final Answer:

    test_example.py -> Option A
  4. 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

  1. Step 1: Recall pytest function naming rule

    Pytest requires test functions to start with test_ to be discovered automatically.
  2. Step 2: Evaluate each function name

    Only test_login() starts with test_. Others do not follow the rule.
  3. Final Answer:

    test_login() -> Option C
  4. 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

  1. 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.
  2. Step 2: Analyze test function behavior

    The test asserts add(2, 3) == 5, which is true, so the test will pass.
  3. Final Answer:

    Run the test function and pass -> Option B
  4. 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

  1. Step 1: Check file naming

    The file math_test.py ends with _test.py, which is valid for pytest discovery.
  2. Step 2: Check function naming

    The function is named add_test(), which does not start with test_. Pytest ignores such functions.
  3. Final Answer:

    The function name does not start with 'test_' -> Option A
  4. 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?
  • File: example_test.py
  • Functions: test_case1(), case2_test(), test_case3()
Which functions will pytest run?
hard
A. None of the functions
B. Only case2_test()
C. All three functions
D. Only test_case1() and test_case3()

Solution

  1. Step 1: Confirm file naming

    The file example_test.py ends with _test.py, so pytest will discover it.
  2. Step 2: Check function naming rules

    Pytest runs functions starting with test_. So test_case1() and test_case3() will run. case2_test() will be ignored.
  3. Final Answer:

    Only test_case1() and test_case3() -> Option D
  4. Quick Check:

    Functions start with 'test_' run = D [OK]
Hint: File ends '_test.py'; functions start 'test_' to run [OK]
Common Mistakes:
  • Thinking '_test' suffix in function name triggers test
  • Assuming all functions in test files run automatically
  • Confusing file and function naming rules
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard