In a previous article, we shared a lightweight unit testing framework applicable to embedded systems! Some friends commented: Does Unity support mock stubbing? Unity does not directly support mock stubbing, but it can be used in conjunction with other tools.

In this article, we will learn about mock stubbing.
In embedded project development, we often encounter these dilemmas:
- The hardware is not ready, and the code that depends on the hardware cannot be debugged;
- The third-party interface/driver is not completed, causing integration issues;
- During unit testing, it is impossible to isolate the tested module from the coupled dependent modules.
And mock stubbing technology is precisely the solution to these problems. It can simulate the behavior of dependent modules, allowing the tested code to run independently of its dependencies, making it one of the core technologies for embedded unit testing and module debugging.
1. What is Mock Stubbing?
1.1 Core Definition
- Mock: Create a “fake” dependent module (such as hardware drivers, third-party interfaces, complex algorithm modules) whose external interface is identical to the real module, but the internal logic is simplified (only meets the needs of the tested module).
- Stub: By technical means, intercept and replace the calls to the real dependent module in the tested code with calls to the Mock module—equivalent to “stubbing” in the call chain, allowing requests to bypass to the Mock module.
The core of embedded Mock stubbing is function call redirection— allowing the CPU to execute the tested code, where the instruction that originally jumps to the real dependent function is changed to jump to the Mock function.

2. What is CMock?
CMock is a tool for automatically generating mocks. Its core value lies in automatically generating complete Mock implementations by parsing header files.
https://github.com/ThrowTheSwitch/CMock
MIT license
CMock is an important member of the ThrowTheSwitch toolchain, closely integrated with Unity.

- Unity: A lightweight C unit testing framework that focuses only on assertions and test case organization.
- CMock: A Ruby-based C header file parser + Mock code generator that helps you disguise dependent modules as controllable fake objects.
The specific example workflow is as follows:

3. Example of CMock + Unity
Tested module: temp_controller.
Module to be mocked: temperature_sensor.

- Prepare Ruby
sudo apt update
sudo apt install -y ruby
- Prepare CMock library
git clone https://github.com/ThrowTheSwitch/CMock
Cloning results in:

As can be seen, Unity is a submodule of CMock. Execute the command <span>git submodule update --init --recursive</span> to initialize and update the submodules in the CMock repository locally:

The root directory of the project needs to create a minimal CMock configuration file <span>cmock_config.yml</span>:

The purpose of the cmock_config.yml file is:

The module to be mocked, such as: temperature_sensor.h

One-click generation of Mock:


Tested module interface such as: temp_controller.h

The temp_controller.c module depends on the temperature_sensor module, and you can use the CMock tool to automatically generate a fake module based on the temperature_sensor header file, which means the code is ready, and temp_controller is a testable module.
Write a unit test file for temp_controller based on the Unity unit testing framework: test_temp_controller.c,
Refer to the example: A lightweight unit testing framework applicable to embedded systems!
For example:


Compile and run:

Conclusion
For embedded projects, the real difficulty in unit testing is not the framework, but whether the relevant isolation has been done well. CMock + Unity provides a systematic and practical path for unit testing: parsing header files to automatically generate Mock, allowing us to focus our time on designing test cases and understanding logic.
Recommended for you:
Several methods for measuring code execution time in embedded systems!
A lightweight unit testing framework applicable to embedded systems!
The correct way to serialize communication in embedded devices!
A lightweight command line shell library for embedded systems!
Click to read the original text, to see the popular embedded books I have filtered for everyone!