Test Overview
This test checks if the function prints the expected message to the console. It uses capsys to capture the printed output and verifies it matches the expected string.
This test checks if the function prints the expected message to the console. It uses capsys to capture the printed output and verifies it matches the expected string.
import pytest def greet(name): print(f"Hello, {name}!") def test_greet_output(capsys): greet("Alice") captured = capsys.readouterr() assert captured.out == "Hello, Alice!\n"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | — | PASS |
| 2 | Calls greet("Alice") function | Function prints 'Hello, Alice!' to standard output | — | PASS |
| 3 | capsys.readouterr() captures printed output | Captured output contains 'Hello, Alice!\n' | — | PASS |
| 4 | Assert captured.out equals 'Hello, Alice!\n' | Captured output matches expected string | assert captured.out == 'Hello, Alice!\n' | PASS |
| 5 | Test ends | Test passed with no errors | — | PASS |
capsys in pytest tests?capsys.readouterr().capture(), getoutput(), or print() do not exist on capsys.def greet():
print('Hello, Tester!')
def test_greet(capsys):
greet()
captured = capsys.readouterr()
assert captured.out == 'Hello, Tester!\n'def test_error(capsys):
print('Start')
captured = capsys.readouterr
assert captured.out == 'Start\n'Pass the test and this pattern moves unknown