I have an Embedded C/C++ project and I want to write unit tests for it with CppUTest. One simple test that I want to do is to ensure that a particular C function is called during the test.
Let's say I have two C functions defined in function.h
:
void success(void)
{
// ... Do Something on success
}
void bid_process(void)
{
bool happy = false;
// ... processing modifiying 'happy' and setting it to 'true'
if (happy)
success(); // Call to success
}
I want to test the function big_process
and I want my test to fail if success
is not called.
For this purpose I wrote some CppUTests in a separate test file test.cpp:
#include <CppUTest/CommandLineTestRunner.h>
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockSupport.h"
#ifdef __cplusplus
extern "C"
{
#include "function.h"
}
#endif
TEST_GROUP(TestGroup)
{
void teardown()
{
mock().clear();
}
};
TEST(TestGroup, Test_big_process)
{
mock().expectOneCall("success"); // success should be called by the call to big process
big_process();
mock().checkExpectations();
}
I checked manually that big_process
is working fine and is calling success
but now I want my test to do it. But the test fails and tells me:
Mock Failure: Expected call did not happen.
EXPECTED calls that did NOT happen:
success -> no parameters
So my question is simple: how to ensure that success
is called during big_process
?