0
0
PyTesttesting~20 mins

Test discovery rules in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pytest Discovery Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Pytest test discovery with file naming
Given the following files in a directory, which files will pytest discover as test files by default?
PyTest
test_example.py
example_test.py
testexample.py
example.py
AAll four files
Btest_example.py only
Ctest_example.py, example_test.py, and testexample.py
Dtest_example.py and example_test.py only
Attempts:
2 left
💡 Hint
Pytest looks for files starting with 'test_' or ending with '_test.py'.
assertion
intermediate
2:00remaining
Pytest test function discovery rules
Which of the following function names will pytest discover as test functions inside a test file?
PyTest
def test_add(): pass
def add_test(): pass
def Test_add(): pass
def testAdd(): pass
Atest_add and add_test
BOnly test_add
Ctest_add, add_test, and Test_add
DAll four functions
Attempts:
2 left
💡 Hint
Pytest looks for functions matching the 'test_*' pattern (starts with 'test_').
🔧 Debug
advanced
2:00remaining
Why is this test function not discovered by pytest?
Consider this test file named test_math.py: ```python def _test_subtract(): assert 5 - 3 == 2 ``` Why does pytest not run this test function?
PyTest
def _test_subtract():
    assert 5 - 3 == 2
AFunction name starts with an underscore, so pytest ignores it
BFunction name must be exactly 'test_subtract' to be discovered
CFunction is missing a return statement
DFunction is missing the @pytest.mark.test decorator
Attempts:
2 left
💡 Hint
Pytest ignores functions starting with underscore by default.
🧠 Conceptual
advanced
2:00remaining
Pytest test class discovery rules
Which of the following classes will pytest discover test methods from inside a test file?
PyTest
class TestCalculator:
    def test_add(self): pass

class CalculatorTest:
    def test_subtract(self): pass

class testCalculator:
    def test_multiply(self): pass

class Test_calculator:
    def test_divide(self): pass
ATestCalculator and Test_calculator only
BAll four classes
CTestCalculator, CalculatorTest, and Test_calculator
DTestCalculator only
Attempts:
2 left
💡 Hint
Pytest discovers classes starting with 'Test' (uppercase T) and ignores others.
framework
expert
2:00remaining
Customizing pytest test discovery patterns
You want pytest to discover test files that start with 'check_' instead of 'test_'. Which pytest configuration option should you set in pytest.ini to achieve this?
A
[pytest]
file_patterns = check_*.py
B
[pytest]
python_functions = check_*
C
[pytest]
python_files = check_*.py
D
[pytest]
test_file_pattern = check_*.py
Attempts:
2 left
💡 Hint
The option to change test file discovery pattern is 'python_files'.