Home | Download CMock | Get Help / Submit Features and Bugs | Learn. Tutorials. Community. | Project Portal |
CMock - Mock Module Generation Framework for C
CMock is a module/object mocking framework for C projects useful for interaction-based unit testing. CMock uses Ruby to auto-generate C source code mock object modules conforming to the interfaces specified in C header files.
It searches your header files for function declarations
ARGS* ParseStuff(char* Cmd); void HandleNeatFeatures(NEAT_FEATURE_T NeatFeature);
and creates a set of Mock objects and helpers
int ParseStuff(char* Cmd); void ParseStuff_ExpectAndReturn(char* Cmd, int toReturn); void ParseStuff_IgnoreAndReturn(int toReturn); void ParseStuff_StubAndCallback(CMOCK_ParseStuff_CALLBACK Callback); void HandleNeatFeatures(NEAT_FEATURE_T* NeatFeature); void HandleNeatFeatures_Expect(NEAT_FEATURE_T* NeatFeature); void HandleNeatFeatures_ExpectWithArrays(NEAT_FEATURE_T* NeatFeature, int NeatFeature_Depth); void HandleNeatFeatures_Ignore(void); void HandleNeatFeatures_StubAndCallback(CMOCK_HandleNeatFeatures_CALLBACK Callback);
which you can use to help write tests
void test_MyFunc_should_ParseStuffAndCallTheHandlerForNeatFeatures(void)
{
NEAT_FEATURES_T ExpectedFeatures = { 1, "NeatStuff" };
ParseStuff_ExpectAndReturn("NeatStuff", 1);
HandleNeatFeatures_Expect(ExpectedFeatures);
//Run Actual Function Under Test
MyFunc("NeatStuff");
}
for other modules
void MyFunc(char* Command)
{
int ID;
NEAT_FEATURES_T Neat;
ID = ParseStuff(Command);
switch(ID)
{
case 0:
HandleStupidFeatures();
break;
case 1:
Neat.id = 1;
Neat.cmd = Command;
HandleNeatFeatures(Neat);
break;
default:
break;
}
}
Automatically generated mock objects allow easy, thorough test coverage in Test-Driven Development and provide the means to efficiently execute Feature-Driven Development techniques such as Presenter First and Conductor First. Presenter First is a technique often used for creating well-tested Graphical User Interfaces. Conductor First is a technique useful for embedded systems software development (see Mocking the Embedded World).
The CMock framework is a set of Ruby scripts and relies on Rake, Ruby's Make-like facility. The auto-generated C code produced by CMock makes use of the C unit testing framework Unity and can even optionally work with exceptions via the CException project.
CMock is easily configurable by tweaking YAML files.
It comes with Ruby/Rake build examples for arbitrary toolchains, by simply modifying or creating a new YAML file for the target toolchain compiler, linker and simulator. We have included YAML configuration files for GCC, and IAR Embedded Workbench for ARM (both v4.x and v5.x). You can try out the IAR toolchain integration with either the full version of IAR EWARM or the 32kB Kickstart Edition which is available for download on the IAR website.