1

I recently got into Unit-Testing embedded C Code. I'd like to test each module fully isolated from all the others. This approach requires me to simulate (or "fake") all the dependencies and external calls a module makes.

Doing just that, I would end up with multiple definitions for the same function -- all the fakes would have the same identifier.

I believe the most common approach to avoid having multiple definitions is to compile one binary for each test -- instead of one big program for all tests.

However this introduces new difficulties. I need a main()-Function for every Module under Test. Also each program now prints its own summary instead of one the total test summary. Oh and its quite tedious to set up the build environment to do this...

There is very likely a smart way to do this. How is it done correctly?

Danish
  • 407
  • 3
  • 10
  • Have you tried https://cmocka.org/ ? – satur9nine Aug 05 '19 at 18:12
  • Correct me if I'm wrong, but I believe each module has its own set of mocks, and each mock is exclusive to that module. If so, why not to declare it as `static`? – Cider Aug 05 '19 at 18:15
  • One alternative is to use an external script or a separate program to run the individual test programs and aggregate their results. There are other possibilities. – John Bollinger Aug 05 '19 at 18:16
  • @Cider If you declare static mocks for each module and use one of these mocks in your module, which functions gets called? The original non-mocked or the static fake? Both are available. AFAIK we still have the problem of multiple definitions. – Danish Aug 05 '19 at 19:22
  • @satur9nine I had a glimpse at cmocka. Correct me if I'm wrong, but I believe that they use wrapping in order to "overwrite" the definition of the original non-mocked function. If its possible to use static on the wrapped functions, then it should be possible to test everything in one handy binary. I believe this is possible. – Danish Aug 05 '19 at 19:26
  • 1
    @Danish I misunderstood your question. Wrappers do seem to be your solution. – Cider Aug 05 '19 at 20:33

0 Answers0