Capturing Output in a Test
Capture output when writing text is part of the public behavior. Inspect return values directly when they carry the result.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Keep output channels separate
A command can write valid records to standard output and diagnostics to standard error. Test the empty-success case and the unreadable-input case. Exit status and output identifies a third observation that callers need.
Compare the intended representation
A report promised one line may need a newline at the end, even when a terminal display makes the difference subtle. Review text representations for escapes and whitespace before normalizing output in an assertion.[1]
Assert the channel of each piece of output
A formatter produces two data rows and a diagnostic warning about a skipped row. Capture standard output and standard error independently. If the warning goes to the data stream, the test can detect contamination despite all rows being present. See output and error streams.
stdout expected: row A, row C
stderr expected: diagnostic for row B
Exit status: according to policy for skipped rows
Decide which formatting is part of the observable contract
A machine-readable output may require exact bytes and final terminators. For a human-facing warning, checking a reason or stable identifier can avoid pinning incidental wording. Test output side effects separately from values returned directly.