Test Overview
This test checks that using a test pattern (like setup and teardown) helps keep tests clean and reliable. It verifies that a simple calculator add function returns correct results consistently.
This test checks that using a test pattern (like setup and teardown) helps keep tests clean and reliable. It verifies that a simple calculator add function returns correct results consistently.
import pytest class Calculator: def add(self, a, b): return a + b @pytest.fixture def calc(): # Setup: create calculator instance calculator = Calculator() yield calculator # Teardown: no resources to free here, but pattern shown def test_addition(calc): result = calc.add(2, 3) assert result == 5 def test_addition_negative(calc): result = calc.add(-1, -1) assert result == -2
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and loads test_addition | Test environment ready, Calculator class loaded | - | PASS |
| 2 | PyTest fixture 'calc' runs setup: creates Calculator instance | Calculator instance created and ready | - | PASS |
| 3 | test_addition calls calc.add(2, 3) | Calculator adds 2 and 3 | Check if result == 5 | PASS |
| 4 | PyTest fixture 'calc' teardown runs (no action needed here) | Calculator instance cleaned up | - | PASS |
| 5 | Test runner loads test_addition_negative | Test environment ready for next test | - | PASS |
| 6 | PyTest fixture 'calc' runs setup again: creates Calculator instance | New Calculator instance created | - | PASS |
| 7 | test_addition_negative calls calc.add(-1, -1) | Calculator adds -1 and -1 | Check if result == -2 | PASS |
| 8 | PyTest fixture 'calc' teardown runs again | Calculator instance cleaned up | - | PASS |