Verify pytest project structure with test discovery
Preconditions (7)
✅ Expected Result: pytest runs the test function inside tests/test_sample.py and reports test passed
import subprocess import sys def test_pytest_project_structure(): # Run pytest command in project_root result = subprocess.run([sys.executable, '-m', 'pytest'], capture_output=True, text=True) output = result.stdout # Check pytest ran tests from tests/test_sample.py assert 'tests/test_sample.py' in output # Check test passed assert '1 passed' in output
This test runs the pytest command in the project root folder using Python's subprocess module.
It captures the output and checks that pytest discovered and ran the test file tests/test_sample.py.
It also asserts that the test passed by looking for '1 passed' in the output.
This confirms the project structure is correct and pytest is configured to find tests only in the tests folder.
Now add a second test file with a different test function and verify pytest runs both tests
tests/ folder in a pytest project?tests/ folder helps keep the project clean and organized.tests/ is a best practice for clarity and maintenance.test_ or end with _test.py.test_example.py starts with test_, so pytest will find it automatically.project/
app.py
tests/
test_math.py
utils/
test_utils.py
Which files will pytest run by default when you run pytest in the project root?tests/ folder and all its subfolders for files starting with test_.test_math.py and test_utils.py match naming rules and are inside tests/ or its subfolder.test_sample.py inside tests/ folder:
def sample_test():
assert 1 + 1 == 2
When you run pytest, no tests are found. Why?test_.sample_test, which does not start with test_, so pytest ignores it.pytest.ini file helps customize test discovery and running specific groups of tests easily.Pass the test and this pattern moves unknown