0
0
PyTesttesting~20 mins

Test naming conventions in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.