Test Overview
This test captures output sent to standard output (stdout) and standard error (stderr) using pytest's capfd fixture. It verifies that the printed messages appear correctly in the captured output.
This test captures output sent to standard output (stdout) and standard error (stderr) using pytest's capfd fixture. It verifies that the printed messages appear correctly in the captured output.
import sys def greet(): print("Hello, stdout!") print("Warning on stderr!", file=sys.stderr) def test_greet_output(capfd): greet() out, err = capfd.readouterr() assert out == "Hello, stdout!\n" assert err == "Warning on stderr!\n"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and pytest initializes the capfd fixture to capture stdout and stderr. | Test environment ready with capfd capturing file descriptors. | - | PASS |
| 2 | Calls the greet() function which prints to stdout and stderr. | Output "Hello, stdout!" sent to stdout and "Warning on stderr!" sent to stderr. | - | PASS |
| 3 | Reads captured output and error using capfd.readouterr(). | Captured stdout contains "Hello, stdout!\n" and stderr contains "Warning on stderr!\n". | - | PASS |
| 4 | Asserts that captured stdout matches expected string. | Captured stdout is "Hello, stdout!\n". | assert out == "Hello, stdout!\n" | PASS |
| 5 | Asserts that captured stderr matches expected string. | Captured stderr is "Warning on stderr!\n". | assert err == "Warning on stderr!\n" | PASS |