Testing CLI output
markand opened this issue · comments
Hello,
I'm trying to create tests for checking that the program outputs correctly what I want. For example, I'd to check that the following output when I call my application:
name: foo
version: 1.0
Since it's multilines output, it's pretty complicated to check line by line. Do you have some recommendation for that purpose?
I'm thinking about creating a file as output expectation and perhaps calling diff
on it in an assert. The only problem is that I'd need to show the diff each time it fails.
Call diff
without assert, redirecting the output to a temporary file. Then check the result of diff
explicitly. In case of a mismatch, cat
the temporary file before you call fail
. In case of a match, call one of the assert
functions with static values, to record the success. Put all that into a custom assert function.
If you don't require diff
output, can't you use the regular assertEquals
with a multi-line string argument?
The tests in shunit2_misc_test.sh
(e.g. testIssue69()) show how this is done for testing shUnit2 itself.
Added an entry to the documentation cookbook.
https://github.com/kward/shunit2/wiki/Cookbook#verifying-command-output