Ceedling: Dealing With Dependency Issue Between Modules

     In general, software cannot be discussed without hardware dependency in embedded system. 



When we need to unit test this type of interface, the microcontroller's specific register or interface function has to be mocked. Interface function can be auto generated as mock function in Ceedling during compile time.

    Currently, src folder contains four files:

led_controller.h                                                led_controller.c

interface_gpio.h

As you can see from led_controller.c, it include interface_gpio.h. This is dependency. 

    Here is test file for led_controller:

This test was verbally mentioned from the first post. This is actual implementation in Ceedling. But this is not enough yet to pass the test. Why?

Let me build and show how it fails:

It is failed because that Ceedling does not know about Write_GPIO function and Read_GPIO function which are coming from interface_gpio module. When we call turn_on_led function, we can expect to call Write_GPIO function. When we call read_led_status function, we can expect to call Read_GPIO function. What we are missing here is mock function to resolve this dependency issue.

How to auto generate mock functions in Ceedling?

It can be done by simply adding prefix "mock_" in test file:

Let me build again:

Ceedling can clearly understand those functions now. It is always stopped at the first found error.

To make it pass, we need to add expected mock function call at exact location.

Finally, unit test is passed:

You can find list of mock function after build: build\test\mocks. As writing unit test, we do not need to see generated c file. Here is auto generated header file:

You might have some curiosity about keyword in mock function such as Expect and ExpectAndReturn. When original function prototype has return type, mock function would be ExpectAndReturn and IgnoreAndReturn. When original function prototype has no return type, mock function would be Expect and Ignore. When you want to verify parameter information, you need to use Expect keyword. Otherwise, you can use Ignore keyword. 

Here is Ignore keyword version when I do not care about mock function's parameter check:

    This post explains how Ceedling handles dependency between two modules. It is handy that Ceedling can automatically generate mock functions for users. It also provides two options from each function whether parameter value is needed to be tested or not.