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 |
capsys to capture stdout and stderr output during tests.capout, capprint, and capture are not valid pytest fixtures.def greet():
print('Hello, Tester!')
def test_greet(capsys):
greet()
captured = capsys.readouterr()
assert captured.out == 'Hello, Tester!\n'def say_hi():
print('Hi!')
def test_say_hi(capsys):
say_hi()
captured = capsys.readouterr()
assert captured.out == 'Hi!'def respond(answer):
if answer == 'yes':
print('Confirmed')
else:
print('Denied')Pass the test and this pattern moves unknown