Bird
Raised Fist0
PyTesttesting~20 mins

Test naming conventions in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Test Naming Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pytest test discovery with naming

Given the following test functions in a file named test_math.py, which tests will pytest discover and run?

PyTest
def test_addition():
    assert 1 + 1 == 2

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

def testMultiply():
    assert 2 * 3 == 6

class TestOperations:
    def test_divide(self):
        assert 6 / 2 == 3
    def divide_test(self):
        assert 6 / 3 == 2
Atest_addition, testMultiply, test_divide
Btest_addition, test_divide
Ctest_addition, check_subtraction, testMultiply, test_divide, divide_test
Dtest_addition, testMultiply, test_divide, divide_test
Attempts:
2 left
💡 Hint

pytest discovers test functions and methods that start with test_ or are in classes named Test* with methods starting with test_.

assertion
intermediate
1:30remaining
Correct assertion for test function name check

Which assertion correctly verifies that a test function name func_name follows pytest naming conventions?

PyTest
func_name = "test_example"
Aassert func_name.startswith("test_")
Bassert func_name.endswith("_test")
Cassert "test" in func_name
Dassert func_name == "testExample"
Attempts:
2 left
💡 Hint

pytest requires test function names to start with test_.

🔧 Debug
advanced
2:00remaining
Identify why pytest skips a test function

Why does pytest skip the following test function during discovery?

PyTest
def Test_login():
    assert True
AFunction is missing a decorator to mark it as a test
BFunction is inside a class without 'Test' prefix
CFunction name is too short
DFunction name does not start with lowercase 'test_'
Attempts:
2 left
💡 Hint

pytest requires test functions to start with test_ in lowercase.

🧠 Conceptual
advanced
2:00remaining
Best practice for test class naming in pytest

Which is the best practice for naming test classes in pytest to ensure test discovery?

AClass names should start with 'Test' and include an __init__ method
BClass names should start with 'test' in lowercase and have an __init__ method
CClass names should start with 'Test' and not have an __init__ method
DClass names can be any name as long as methods start with 'test_'
Attempts:
2 left
💡 Hint

pytest discovers test classes starting with 'Test' and ignores classes with __init__ methods.

framework
expert
2:30remaining
Effect of pytest.ini on test naming conventions

Given the following pytest.ini configuration, which test function name will pytest discover?

[pytest]
testpaths = tests
python_files = check_*.py
python_functions = check_*
Adef check_addition(): pass
Bdef test_addition(): pass
Cdef addition_check(): pass
Ddef add_check(): pass
Attempts:
2 left
💡 Hint

pytest.ini overrides default test discovery patterns.

Practice

(1/5)
1. Which of the following is the correct way to name a test function in pytest so it is automatically discovered?
easy
A. test_login_functionality
B. check_login_functionality
C. LoginTest
D. verifyLogin

Solution

  1. Step 1: Understand pytest test discovery rules

    pytest automatically finds test functions that start with test_.
  2. Step 2: Check each option against the rule

    Only test_login_functionality starts with test_, so pytest will find it automatically.
  3. Final Answer:

    test_login_functionality -> Option A
  4. Quick Check:

    Test names start with test_ = C [OK]
Hint: Test names must start with test_ to be found [OK]
Common Mistakes:
  • Not starting test names with test_
  • Using camelCase instead of snake_case
  • Using class names instead of functions
2. Which of the following test function names is syntactically correct in pytest?
easy
A. test-user-login
B. test-UserLogin
C. test_user_login
D. test user login

Solution

  1. Step 1: Identify valid Python function names

    Function names must use letters, numbers, and underscores only; no spaces or hyphens.
  2. Step 2: Check each option

    test_user_login uses underscores and is valid; others use hyphens, spaces which are invalid.
  3. Final Answer:

    test_user_login -> Option C
  4. Quick Check:

    Underscores allowed in function names = D [OK]
Hint: Use underscores, no spaces or hyphens in test names [OK]
Common Mistakes:
  • Using hyphens in function names
  • Including spaces in test names
  • Using camelCase instead of snake_case
3. Given the following pytest code, which test functions will pytest discover and run?
def test_addition():
    assert 1 + 1 == 2

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

def testMultiplication():
    assert 2 * 3 == 6

class TestDivision:
    def test_divide(self):
        assert 6 / 2 == 3
medium
A. test_addition and test_divide only
B. All four functions
C. test_addition, testMultiplication, and test_divide
D. test_addition only

Solution

  1. Step 1: Identify pytest discovery rules for functions and methods

    pytest finds functions starting with test_ and methods inside classes named Test* starting with test_.
  2. Step 2: Analyze each function/method

    test_addition is found; check_subtraction is ignored; testMultiplication is ignored due to camelCase; test_divide inside TestDivision class is found.
  3. Final Answer:

    test_addition and test_divide only -> Option A
  4. Quick Check:

    Functions/methods start with test_ = A [OK]
Hint: pytest finds test_ functions and test_ methods in Test* classes [OK]
Common Mistakes:
  • Assuming camelCase test names are found
  • Ignoring test methods inside Test classes
  • Thinking all functions are discovered
4. You wrote a test function named Test_login but pytest does not run it. What is the most likely reason?
medium
A. Function is inside a class without Test prefix
B. Function name contains uppercase letters
C. Function is missing parentheses
D. Function name does not start with lowercase test_

Solution

  1. Step 1: Recall pytest naming rules for functions

    pytest requires test functions to start with lowercase test_ exactly.
  2. Step 2: Analyze the function name Test_login

    It starts with uppercase T, so pytest ignores it.
  3. Final Answer:

    Function name does not start with lowercase test_ -> Option D
  4. Quick Check:

    Lowercase test_ prefix required = A [OK]
Hint: Test function names must start with lowercase test_ [OK]
Common Mistakes:
  • Using uppercase T in test function names
  • Assuming pytest finds any function with 'test' inside
  • Ignoring function naming case sensitivity
5. You want to write clear pytest test names that describe what they check. Which of the following is the best test function name for checking if a user can log in with valid credentials?
hard
A. test1
B. test_valid_user_login
C. test_login
D. test_user_login_with_valid_credentials_and_session_creation

Solution

  1. Step 1: Consider clarity and brevity in test names

    Good test names are clear but not overly long or vague.
  2. Step 2: Evaluate each option

    test_valid_user_login clearly states the test purpose concisely. test1 is vague. test_login is too general. test_user_login_with_valid_credentials_and_session_creation is too long and hard to read.
  3. Final Answer:

    test_valid_user_login -> Option B
  4. Quick Check:

    Clear and concise descriptive names = B [OK]
Hint: Choose clear, concise test names describing the check [OK]
Common Mistakes:
  • Using vague or generic names like test1
  • Writing overly long test names
  • Using names that don't describe the test purpose