ThrowTheSwitch / Ceedling

Ruby-based unit testing and build system for C projects

Home Page:http://throwtheswitch.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

unit test an output message written in the console with printf

TechLeadYoda opened this issue · comments

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

Use fflush(stdout)

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

@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.