Given the following test functions in a file named test_math.py, which tests will pytest discover and run?
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
pytest discovers test functions and methods that start with test_ or are in classes named Test* with methods starting with test_.
pytest discovers functions starting with test_ and methods in classes named Test* starting with test_. So test_addition, testMultiply (not starting with test_), and test_divide are considered. However, testMultiply does not start with test_ so it is not discovered. The correct discovered tests are test_addition and test_divide. But testMultiply is not discovered. So only test_addition and test_divide run. Option B incorrectly includes testMultiply. Reconsider options carefully.
Which assertion correctly verifies that a test function name func_name follows pytest naming conventions?
func_name = "test_example"pytest requires test function names to start with test_.
The correct assertion checks if the function name starts with test_. Option A does this correctly. Option A checks the wrong end of the string. Option A is too broad and allows names like mytestfunc. Option A checks for an exact name which is not general.
Why does pytest skip the following test function during discovery?
def Test_login(): assert True
pytest requires test functions to start with test_ in lowercase.
pytest discovers test functions only if their names start with lowercase test_. Here, the function name starts with uppercase T, so pytest skips it.
Which is the best practice for naming test classes in pytest to ensure test discovery?
pytest discovers test classes starting with 'Test' and ignores classes with __init__ methods.
pytest discovers test classes whose names start with 'Test' (uppercase T) and which do not define an __init__ method. Having an __init__ method prevents pytest from collecting tests in that class.
Given the following pytest.ini configuration, which test function name will pytest discover?
[pytest] testpaths = tests python_files = check_*.py python_functions = check_*
pytest.ini overrides default test discovery patterns.
The configuration changes pytest to look for files starting with 'check_' and functions starting with 'check_'. So only functions named like 'check_addition' are discovered. 'test_addition' is ignored because 'python_functions' is set to 'check_*'.