Testing Output Streams

libcester allows the testing of file stream without any intrusion in the code. With this feature you can simply output any value using printf, fprintf, puts or any other stream method and validate the output without actually writing to those streams. An example is writing some ansi code into the trminal using printf but in your unit test you want to actually validate what is written to the terminal without actually outputing it into the terminal.

The following macros are used for testing file streams.

Ensure a stream that was captured is freed after the assertion or at the end of a test case, it bad practice to capture a stream outside a test case at it might be impossible to track the stream and ensure it properly released.

Standard streams helper macros

There are helpers macros to quickly works on the standard streams stdout, stderr, stdin.

Testing stdout stream

The stdout stream is where data written to printf, puts is sent to by default, the data sent can be validated using the stream assertion functions.

The example below tests the data written to stdout

#include <exotic/cester.h>

CESTER_TEST(test_stdout, test_inst,
        CESTER_CAPTURE_STDOUT();

        printf("This will not be shown in terminal\n");
        puts("Because it has been captured by libester");
        cester_assert_stdout_stream_content_equal("This will not be shown in terminal\nBecause it has been captured by libester\n");
        cester_assert_stdout_stream_content_contain("terminal");

        CESTER_RESET_STDOUT();
        puts("Hello World");
        cester_assert_stdout_stream_content_equal("Hello World\n");

        CESTER_RELEASE_STDOUT();
)

Compiling the test above will give the following output

+ (0.02s) test stdout


Ran 1 test(s) in 0.02 Seconds
Synthesis: SUCCESS Tests: 1 | Passing: 1 | Failing: 0

In the above test as seen in the code printf is used as it will be used in regular programs but because it been captured cester. the test case is able to make some assertion on the data written, the data will not be written to the terminal as stdout is not itself at that point.

Note

  • CESTER_CAPTURE_STDOUT() is alias for CESTER_CAPTURE_STREAM(stdout)
  • CESTER_RESET_STDOUT() is alias for CESTER_RESET_STREAM(stdout)
  • CESTER_RELEASE_STDOUT() is alias for CESTER_RELEASE_STREAM(stdout)

It required to be explicit with the stream to capture if no helper macro is provider by cester.

Also the stream was reset with the macro CESTER_RESET_STDOUT so the old data is completely removed and fresh data can be written to the stream, else if the stream is not reset the new data will simply be appended to the existing data.

Warning

if the system the test case is executed on does not have a file system, has very limited file system or does not allow read and/or write to the /tmp folder, libcester will not be able to capture the stream and the stream assertions will fail, and it even possible for the test case to segfault.