Test Overview
This test package contains two simple test files. It verifies that pytest can discover and run tests across multiple files inside a package.
This test package contains two simple test files. It verifies that pytest can discover and run tests across multiple files inside a package.
import pytest # File: tests/test_math.py def test_addition(): assert 1 + 1 == 2 def test_subtraction(): assert 5 - 3 == 2 # File: tests/test_strings.py def test_uppercase(): assert 'hello'.upper() == 'HELLO' def test_isdigit(): assert '1234'.isdigit() == True # To run tests, execute: pytest tests/
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and looks for tests in the 'tests' package | Pytest scans the 'tests' directory and finds test_math.py and test_strings.py | — | PASS |
| 2 | Pytest loads test_math.py and runs test_addition | Running test_addition function | Assert 1 + 1 == 2 | PASS |
| 3 | Pytest runs test_subtraction in test_math.py | Running test_subtraction function | Assert 5 - 3 == 2 | PASS |
| 4 | Pytest loads test_strings.py and runs test_uppercase | Running test_uppercase function | Assert 'hello'.upper() == 'HELLO' | PASS |
| 5 | Pytest runs test_isdigit in test_strings.py | Running test_isdigit function | Assert '1234'.isdigit() == True | PASS |
| 6 | Pytest finishes running all tests and reports results | All 4 tests passed | — | PASS |
pytest?__init__.py file.__init__.py file -> Option A__init__.py file as packages, including test packages.__init__.py inside the folder -> Option Ctests/ __init__.py test_math.py test_string.pyWhat will happen when you run
pytest tests/?__init__.py marks the folder as a package but does not prevent pytest from running tests inside.test_math.py and test_string.py -> Option Dtests/ with test files but forgot to add __init__.py. What issue might you face?__init__.py, the folder is not a recognized Python package, so pytest may not treat it as a test package.__init__.py files like this:
tests/
__init__.py
unit/
__init__.py
test_utils.py
integration/
__init__.py
test_api.py
How can you run only the integration tests using pytest?__init__.py files mark subfolders as packages but do not affect running tests by folder path.pytest tests/integration/ to run only integration tests -> Option BPass the test and this pattern moves unknown