Skip to content

Commit

Permalink
Capture the tests' stderr output
Browse files Browse the repository at this point in the history
This will show the stderr in the failure report only and leave a
clean test run.

Also, the makefile with .DEFAULT was not compiling, so let's
use a parametrable test target
  • Loading branch information
Martin Roy committed Nov 23, 2016
1 parent 2dc5b15 commit 2c1384f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,4 @@ compile:
chmod +x target/sbtest.sh

test: compile
target/sbtest.sh

.DEFAULT: compile
target/sbtest.sh $@
target/sbtest.sh ${TEST}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ Running the tests:

you can run only one suite/test with

make suite.test
make test TEST=suite.test
6 changes: 4 additions & 2 deletions src/test-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ for test in ${tests}; do

failed=0

${test} > ${workspace}/test_output || true
${test} >${workspace}/test_output 2>${workspace}/test_output_err || true

if [ ! -f ${workspace}/.assertion_error ]; then
echo "OK"
Expand All @@ -74,8 +74,10 @@ for test in ${tests}; do
=========================
FAIL: ${test_name}
-------------------------
-------- STDOUT ---------
$(cat ${workspace}/test_output)
-------- STDERR ---------
$(cat ${workspace}/test_output_err)
-------------------------
FAILURE

Expand Down
6 changes: 6 additions & 0 deletions test-fixtures/failing-test/src/fail-with-stderr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

echo "This is stuff" 1>&2
echo "from stderr" 1>&2

exit 1
5 changes: 5 additions & 0 deletions test-fixtures/failing-test/test/test_failing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ test_that_fails() {
(bash fail.sh)
assert ${?} succeeded
}

test_that_fails_with_stderr() {
(bash fail-with-stderr.sh)
assert ${?} succeeded
}
39 changes: 38 additions & 1 deletion test/test_runner_output.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ test_output_is_fine_when_a_test_fails() {
cp -aR ${TEST_ROOT_DIR}/../test-fixtures/failing-test/* .

unset RUN_SINGLE_TEST
actual=$(${TEST_ROOT_DIR}/../target/sbtest.sh)
actual=$(${TEST_ROOT_DIR}/../target/sbtest.sh failing.that_fails)
assert ${?} failed

expected=$(cat <<-EXP
Expand All @@ -96,9 +96,46 @@ failing.that_fails...FAILED
=========================
FAIL: failing.that_fails
-------- STDOUT ---------
Expected success exit code
Got: <1>
-------- STDERR ---------
-------------------------
-------------------------
Ran 1 test
>>> FAILURE (1 error) <<<
EXP
)
assert "${actual}" equals "${expected}"
}

test_output_also_show_stderr_when_a_test_fails() {

cp -aR ${TEST_ROOT_DIR}/../test-fixtures/failing-test/* .

unset RUN_SINGLE_TEST
actual=$(${TEST_ROOT_DIR}/../target/sbtest.sh failing.that_fails_with_stderr)
assert ${?} failed

expected=$(cat <<-EXP
Running Simple Bash Tests
-------------------------
failing.that_fails_with_stderr...FAILED
=========================
FAIL: failing.that_fails_with_stderr
-------- STDOUT ---------
Expected success exit code
Got: <1>
-------- STDERR ---------
This is stuff
from stderr
-------------------------
-------------------------
Expand Down

0 comments on commit 2c1384f

Please sign in to comment.