Framework Mode - Test file and function naming conventions
Folder Structure
project-root/ ├── tests/ │ ├── test_login.py │ ├── test_shopping_cart.py │ ├── test_checkout.py │ └── __init__.py ├── src/ │ └── app_code.py ├── conftest.py └── pytest.ini
project-root/ ├── tests/ │ ├── test_login.py │ ├── test_shopping_cart.py │ ├── test_checkout.py │ └── __init__.py ├── src/ │ └── app_code.py ├── conftest.py └── pytest.ini
test_ (e.g., test_login.py), containing test functions also starting with test_.conftest.py holds shared setup code and fixtures.src/ folder.pytest.ini for pytest settings.pytest.ini to configure test discovery patterns, e.g., python_files = test_*.py and python_functions = test_*.test_ prefix to be auto-discovered by pytest.conftest.py for shared fixtures and environment setup.pytest-html to generate readable HTML reports.pytest command.test_ and end with .py for pytest to find them.test_ to be recognized as tests.conftest.py for shared fixtures instead of repeating setup code.Where would you add a new test function for verifying user registration in this framework structure?
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