Test Overview
This test module contains two simple test functions to verify basic arithmetic operations. It checks if addition and multiplication work as expected.
This test module contains two simple test functions to verify basic arithmetic operations. It checks if addition and multiplication work as expected.
import pytest def test_addition(): result = 2 + 3 assert result == 5 def test_multiplication(): result = 4 * 5 assert result == 20
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Pytest starts test discovery and finds test_addition function | Test module loaded with two test functions | — | PASS |
| 2 | test_addition function runs: calculates 2 + 3 | Calculation result stored in variable | Assert that result == 5 | PASS |
| 3 | test_addition completes successfully | Test passed | — | PASS |
| 4 | Pytest runs test_multiplication function: calculates 4 * 5 | Calculation result stored in variable | Assert that result == 20 | PASS |
| 5 | test_multiplication completes successfully | Test passed | — | PASS |
| 6 | Pytest finishes running all tests in the module | All tests passed | — | PASS |
test_ or end with _test.py.test_ or end with _test.py. matches the correct naming pattern for pytest to find test modules automatically.test_ or end with _test.py. -> Option Dtest_ automatically as tests.test_, so pytest will recognize it as a test.test_sample.py with these functions:def test_add():
assert 1 + 1 == 2
def add():
return 1 + 1
def test_subtract():
assert 2 - 1 == 1
test_ automatically as tests.test_add and test_subtract start with test_, so pytest runs them. add does not start with test_, so it is ignored.test_add and test_subtract -> Option Ctest_math.py with this code:def test_multiply():
assert 2 * 3 == 5
test_add.py and test_subtract.py. Each file has multiple test functions starting with test_. How does pytest handle these test modules when you run pytest in the folder?test_ prefix and runs all test functions inside them.pytest in the folder, it collects tests from both test_add.py and test_subtract.py and runs all test functions starting with test_.Pass the test and this pattern moves unknown