kward / shunit2

shUnit2 is a xUnit based unit test framework for Bourne based shell scripts.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

If the actual value is "", then assertEquals() reports expected/actual incorrectly

jbrains opened this issue · comments

It looks like assertEquals() can't tell the difference between "parameter 3 is not set, so assume that parameter 1 is the expected result and parameter 2 is the actual result" and "parameter 3 is set, but empty, so assume that parameter 1 is the failure message, parameter 2 is the expected result, and parameter 3 is the actual result".

I don't know bash deeply enough to know whether fixing this problem creates others. It seems that putting the failure message at the end would make the whole problem go away, even though it would annoy clients. Maybe for shunit3?

This looks similar to #53, and the fix is to wrap your expected output in " chars.

I'm guessing you were doing something like this (script saved as test_equals.sh).

#!/bin/bash
cmd() { echo ''; }

test_cmd1() {
  assertEquals "cmd() failed" "" $(cmd)
}
test_cmd2() {
  output=$(cmd)
  assertEquals "cmd() failed" "" ${output}
}

. ~/lib/sh/shunit2

This will produce the behavior you are seeing.

$ ./test_equals.sh 
test_cmd1
ASSERT:expected:<cmd() failed> but was:<>
test_cmd2
ASSERT:expected:<cmd() failed> but was:<>

Ran 2 tests.

FAILED (failures=2)

Wrapping the output fixes the problem in both cases.

#!/bin/bash
cmd() { echo ''; }

test_cmd1() {
  assertEquals "cmd() failed" "" "$(cmd)"
}
test_cmd2() {
  output=$(cmd)
  assertEquals "cmd() failed" "" "${output}"
}

. ~/lib/sh/shunit2
$ ./test_equals.sh 
test_cmd1
test_cmd2

Ran 2 tests.

OK