Failing with Message Always Prints As-Is to Standard Error
Calling fail_msg
in a test will cause the message to be printed as-is to Standard Error. The message is not carried over to the test output, which is especially noticable in the STDOUT
and XML
output formats.
Consider the following minimal example: cmocka_issue.c. I compiled and ran as
$ gcc -I/cmocka/install/dir/include/ -L/cmocka/install/dir/lib -lcmocka -o cmocka_issue.out cmocka_issue.c
$ LD_PRELOAD=/cmocka/install/dir/lib/libcmocka.so ./cmocka_issue.out
The output I got was
[==========] tests: Running 1 test(s).
[ RUN ] always_fail
ERROR: Failure from always_fail
[ ERROR ] --- [ LINE ] --- cmocka_issue.c:11: error: Failure!
[ FAILED ] always_fail
[==========] tests: 1 test(s) run.
[ PASSED ] 0 test(s).
[ FAILED ] tests: 1 test(s), listed below:
[ FAILED ] always_fail
1 FAILED TEST(S)
Note how the ERROR:
line is not integrated into the output. The issue is worse when run with XML
:
$ CMOCKA_MESSAGE_OUTPUT=XML LD_PRELOAD=$HOME/Desktop/cmocka/lib/libcmocka.so ./cmocka_issue.out
ERROR: Failure from always_fail
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites>
<testsuite name="tests" time="0.000" tests="1" failures="1" errors="0" skipped="0" >
<testcase name="always_fail" time="0.000" >
<failure><![CDATA[cmocka_issue.c:11: error: Failure!]]></failure>
</testcase>
</testsuite>
</testsuites>
There is a line before the output, and more importantly the <failure>
data does not have the message I wrote.
The expected output in the above two cases would be
[==========] tests: Running 1 test(s).
[ RUN ] always_fail
[ ERROR ] --- ERROR: Failure from always_fail
[ LINE ] --- cmocka_issue.c:11: error: Failure!
[ FAILED ] always_fail
[==========] tests: 1 test(s) run.
[ PASSED ] 0 test(s).
[ FAILED ] tests: 1 test(s), listed below:
[ FAILED ] always_fail
and
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites>
<testsuite name="tests" time="0.000" tests="1" failures="1" errors="0" skipped="0" >
<testcase name="always_fail" time="0.000" >
<failure><![CDATA[ERROR: Failure from always_fail
cmocka_issue.c:11: error: Failure!]]></failure>
</testcase>
</testsuite>
</testsuites>
respectively.
Thankfully, the fix I've found is fairly straightforward. Simply modify the fail_msg
macro to use cm_print_error
instead of print_error
, and include that function in the header file as well. A modified header is: cmocka.h, and a Merge Request will be coming shortly.