Test Overview
This test runs a function that prints a message. It captures the printed output and checks if it matches the expected text. This shows how capturing output helps confirm the program behaves correctly.
This test runs a function that prints a message. It captures the printed output and checks if it matches the expected text. This shows how capturing output helps confirm the program behaves correctly.
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 | Test environment is ready with pytest and capsys fixture available | - | PASS |
| 2 | Calls greet("Alice") function | Function prints 'Hello, Alice!' to standard output | - | PASS |
| 3 | Captures printed output using capsys.readouterr() | Captured output contains 'Hello, Alice!\n' | - | PASS |
| 4 | Asserts captured output equals 'Hello, Alice!\n' | Captured output matches expected string exactly | assert captured.out == 'Hello, Alice!\n' | PASS |
| 5 | Test ends successfully | Test passed with no errors | - | PASS |