Verify pytest recognizes test files and functions by naming conventions
Preconditions (2)
✅ Expected Result: pytest discovers and runs the test_addition function inside test_sample.py and reports the test as passed
import pytest def test_addition(): assert 2 + 2 == 4 # To run this test, save it in a file named test_sample.py # Then run: pytest # pytest will automatically discover this test because the file and function names start with 'test_'
The code defines a test function named test_addition inside a file named test_sample.py. This follows pytest's naming conventions.
pytest automatically finds files starting with test_ and functions starting with test_. The assertion checks a simple math operation.
When you run pytest, it discovers and runs this test, reporting success if the assertion passes.
Now create three test functions in the same file to test addition, subtraction, and multiplication using correct naming conventions.
test_ or end with _test.py.test_, so it is correct. Options A, C, and D do not follow the naming convention.test_ to be discovered automatically.test_. Others do not follow the rule.test_math.py with this code:def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5
What will pytest do when run on this file?test_math.py and the function is named test_add, both following pytest conventions.add(2, 3) == 5, which is true, so the test will pass.math_test.py with a function named add_test(). When running pytest, no tests are found. What is the most likely cause?math_test.py ends with _test.py, which is valid for pytest discovery.add_test(), which does not start with test_. Pytest ignores such functions.example_test.pytest_case1(), case2_test(), test_case3()example_test.py ends with _test.py, so pytest will discover it.test_. So test_case1() and test_case3() will run. case2_test() will be ignored.Pass the test and this pattern moves unknown