Bird
Raised Fist0
PyTesttesting~20 mins

Test discovery rules in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 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'.

Practice

(1/5)
1. Which of the following file names will pytest recognize as a test file by default?
easy
A. example.py
B. test_example.py
C. example_test.pyc
D. TestExample.py

Solution

  1. Step 1: Understand pytest file naming rules

    pytest looks for files starting or ending with 'test' and ending with '.py'.
  2. Step 2: Check each option

    'test_example.py' starts with 'test' and ends with '.py', so pytest will find it. Others either don't end with '.py' or don't start/end with 'test'.
  3. Final Answer:

    test_example.py -> Option B
  4. Quick Check:

    File starts or ends with 'test' + '.py' = test_example.py [OK]
Hint: Test files start or end with 'test' and end with '.py' [OK]
Common Mistakes:
  • Thinking pytest finds .pyc files
  • Ignoring case sensitivity in file names
  • Assuming any .py file is a test file
2. Which function name will pytest recognize as a test function by default?
easy
A. test_login()
B. testLogin()
C. check_login()
D. login_test()

Solution

  1. Step 1: Recall pytest test function naming

    pytest looks for functions starting with 'test_'.
  2. Step 2: Evaluate each function name

    'test_login()' starts with 'test_' exactly, so pytest will find it. Others do not follow this pattern.
  3. Final Answer:

    test_login() -> Option A
  4. Quick Check:

    Function starts with 'test_' = test_login() [OK]
Hint: Test functions start with 'test_' exactly [OK]
Common Mistakes:
  • Using camelCase instead of snake_case
  • Placing 'test' at the end of function name
  • Missing underscore after 'test'
3. Given the following files and functions, which tests will pytest discover and run?
File: test_math.py
  def test_add(): pass
  def add_test(): pass

File: math_test.py
  def test_subtract(): pass
  def subtract_test(): pass

File: math.py
  def test_multiply(): pass
medium
A. test_add, test_subtract
B. test_add, test_subtract, test_multiply
C. test_add, subtract_test
D. add_test, subtract_test

Solution

  1. Step 1: Identify test files pytest finds

    pytest finds files starting or ending with 'test' and ending with '.py'. So 'test_math.py' and 'math_test.py' are test files. 'math.py' is not.
  2. Step 2: Identify test functions pytest finds in those files

    In 'test_math.py', pytest finds 'test_add' (starts with 'test_'). 'add_test' is ignored. In 'math_test.py', pytest finds 'test_subtract'. 'subtract_test' is ignored. 'test_multiply' in 'math.py' is ignored because file is not a test file.
  3. Final Answer:

    test_add, test_subtract -> Option A
  4. Quick Check:

    Files and functions follow naming rules = test_add, test_subtract [OK]
Hint: Test files and functions must follow naming rules to be discovered [OK]
Common Mistakes:
  • Assuming any function starting with 'test' is found regardless of file name
  • Including functions in non-test files
  • Confusing function name endings with beginnings
4. You have a test file named TestExample.py with a function def test_func():. pytest does not run this test. What is the likely cause?
medium
A. Function name does not start with 'test_'
B. pytest requires test classes to run tests
C. File name does not follow pytest discovery rules
D. Function is missing a return statement

Solution

  1. Step 1: Check pytest file discovery rules

    pytest finds files starting or ending with 'test' in lowercase and ending with '.py'. 'TestExample.py' starts with uppercase 'T', so it is not recognized.
  2. Step 2: Verify function name and other options

    Function name 'test_func' is correct. Return statement is not required. pytest does not require test classes to run functions.
  3. Final Answer:

    File name does not follow pytest discovery rules -> Option C
  4. Quick Check:

    File name case-sensitive for discovery = D [OK]
Hint: File names must be lowercase starting or ending with 'test' [OK]
Common Mistakes:
  • Ignoring case sensitivity in file names
  • Thinking function needs return statement
  • Assuming test classes are mandatory
5. You want pytest to discover tests in a file named mytests.py containing functions def test_one(): and def test_two():. By default, pytest does not find this file. How can you rename the file to follow pytest discovery rules without changing function names?
hard
A. Rename to my_test_s.py
B. Rename to mytestsTest.py
C. Rename to testmytests.py
D. Rename to test_mytests.py

Solution

  1. Step 1: Recall pytest file discovery naming

    pytest finds files starting with 'test_' or ending with '_test.py'.
  2. Step 2: Check options for correct naming

    'test_mytests.py' starts with 'test_' [check]. 'mytestsTest.py' ends with 'Test.py' (uppercase 'T' not matching lowercase 'test'). 'testmytests.py' lacks '_' after 'test'. 'my_test_s.py' does not start with 'test_' or end with '_test.py'. Only the first option works.
  3. Final Answer:

    Rename to test_mytests.py -> Option D
  4. Quick Check:

    File starts or ends with 'test' = test_mytests.py [OK]
Hint: File must start or end with 'test' exactly [OK]
Common Mistakes:
  • Removing 'test' from file name
  • Adding 'test' inside word without underscore
  • Using camelCase instead of snake_case
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