Verify pytest test function naming conventions
Preconditions (2)
✅ Expected Result: pytest runs only the function(s) whose names start with 'test_' and ignores others
Jump into concepts and practice - no test required
import pytest collected_tests = [] # Define a test function with correct naming def test_example_function(): assert True # Define a function that should not be collected as a test def example_function(): assert False def test_naming_conventions(pytestconfig): # Use pytest's collection mechanism to get test names session = pytest.Session.from_config(pytestconfig) session.perform_collect() for item in session.items: collected_tests.append(item.name) # Assert that only functions starting with 'test_' are collected assert 'test_example_function' in collected_tests assert 'example_function' not in collected_tests
This script defines two functions: one named with the 'test_' prefix and one without.
pytest only collects and runs functions starting with 'test_'.
The test_naming_conventions function uses pytest's collection to check which functions are collected.
Assertions confirm that only the correctly named test function is collected and run.
This demonstrates the importance of naming conventions in pytest.
Create three test functions with different valid names starting with 'test_' and verify pytest runs all three.
test_.test_, so pytest will find it automatically.def test_addition():
assert 1 + 1 == 2
def check_subtraction():
assert 2 - 1 == 1
def testMultiplication():
assert 2 * 3 == 6
class TestDivision:
def test_divide(self):
assert 6 / 2 == 3
test_ and methods inside classes named Test* starting with test_.test_addition is found; check_subtraction is ignored; testMultiplication is ignored due to camelCase; test_divide inside TestDivision class is found.Test_login but pytest does not run it. What is the most likely reason?test_ exactly.Test_loginT, so pytest ignores it.