You are here
Home > embedded >

How To Mock Local Variable In Ceedling

In embedded system product development there are quite number of tools and framework for unit testing the C code. In this blog post I will explain about how to mock local variable in ceedling for unit testing. Ceedling is one of the widely used open source framework for your test driven product development (TDD) in embedded systems.


What is Ceedling

‘Ceedling’ is open source framework used in unit testing, in our project which involves embedded system development we are using ‘ceedling’ framework. Ceedling works together with Unity and CMock. Both are are open source tools for testing C code. Ceedling has feature of automatic generation of mock. Read more about ceedling features in our previous blog post.

Unit Testing Tools

Unit testing using framework calls small unit of code or stubs that calls unit code to check if it performing as expected. Ceedling is a build system specifically designed for running unit tests for embedded code written in C language. There are other unit test frameworks like google gTest an xUnit Test Framework, then Vectorcast – Test Automation Solution for Embedded Software Developers to Validate Systems. Googletest or gTest is more suited for application software written in high level languages like C++. One can make use of gtest for C language, but it has its own advantages. Vectorcast is targeted for high quality embedded products like automotive, and it is not freeware, so comes with license cost. However I have personally used Vectorcast and its GUI is pretty nice which automates most of your work right from test stub creation, test coverage reports and suggestions.

In embedded system product development unit testing is not easy as it is associated with hardware. Check out our post on how we have tweaked this framework to mock port pins.

How To Mock Local Variable In Ceedling

Problem Statement

I was testing function for one module which is using the modbus APIs defined in modbus.c file. For ceedling unit testing I mocked low level APIs like modbus_read_registers() and modbus_write_register().

In the code I had defined function int _readModbusRegister( ) and function int _writeModbusRegister() inside which mobus_read/write_registers APIs are used.

This image has an empty alt attribute; its file name is read_Modbus_func1.jpg

The function under test is:

In above function I need to test all the possible conditions for different value of ‘drvStatus’ variable. But ‘drvStatus’ is local variable. How can I mock the local variable in Ceedling? We cannot directly mock local variable using Ceedling. So in this case if we made our mock function modbus_read_registers() return 1 then user defined function _readModbusRegister() will always return ‘regValue’ as zero. Because right now we are testing the code without using hardware. So always ‘drvStatus’ variable will become zero and code will not get execute further. And we cannot change the value of drvStatus with this conditions.

Solution :

In ‘Ceedling’ generally we have to mock API but for getting different value for local variable ‘drvStatus’ in this case we have to mock the function which is returning value to local variable, i.e. in this case we will mock ‘_readModbusRegister()’. The actual function which we need to mock is always returning the register value. So add these two user defined function declarations to a file and add this file prefixing _mock .Ceedling will automatically create the mock versions of given functions.

When we mock the function whose definition is present we have to comment that definition otherwise ceedling will throw error for ‘ found multiple definitions’. I commented the definitions for these two mocked functions. Now I can use these _readModbusRegister_ExpectAndReturn() function with any return value and can generate test case for every possible condition. Like in first test case we are returning 8 to ‘drivestatus’ .

Here in second test case we can return 16 to ‘drivestatus variable’.

Let us know if you know any other workaround for on How To Mock Local Variable with Ceedling unit testing framework.

Leave a Reply