Ceedling icon indicating copy to clipboard operation
Ceedling copied to clipboard

unit test an output message written in the console with printf

Open TechLeadYoda opened this issue 1 year ago • 3 comments

test

TechLeadYoda avatar Apr 04 '24 16:04 TechLeadYoda

Hello, i have trouble testing with ceedling this simple code. I ve tried to redirect the stdout to a file and after that I put the file contain in the string array . It's working but it is cluttered as a result by the test summary output. So I don't have the result of the test printed in the terminal. Could you please help me to find an easier solution if possible. Thanks for your advices.

TechLeadYoda avatar Apr 04 '24 19:04 TechLeadYoda

@TechLeadYoda

Use fflush(stdout)

void test_myFunc1(void)
{
  fflush(stdout);
  ...
  ...
}

kafkaaaa avatar Apr 15 '24 01:04 kafkaaaa

@TechLeadYoda Generally speaking, directly testing stream handling with unit testing frameworks is discouraged. As you've already noticed it gets noisy and particularly in the case of files it becomes quite hard to manage the test set up and teardown.

The tried and true approach is:

  • Write your code to test just up to the stream handling but not the stream handling itself. This can mean placing stream handling behind a wrapper you mock or simply separating your buffer handling from a simple handoff to stream handling. In the case of a printf() you might write code to prepare the string buffers that will be handed off to be printed and only unit test the string buffer handling.
  • Use manual validation to verify the end result of the stream handling or begin relying on a system test framework (e.g. the Robot or Cucumber frameworks) to test your compiled and linked application with its output streams.

mkarlesky avatar May 10 '24 18:05 mkarlesky

TODO: @mkarlesky Add this discussion to Ceedling cookbook of testing needs and solutions.

mkarlesky avatar Jul 10 '24 14:07 mkarlesky