Unit Testing Embedded Code With Cmocka
Unit Testing Embedded Code With Cmocka
http://www.samlewis.me/2016/09/embedded-unit-testing-with-cmocka/ Page 1 of 8
Unit testing embedded code with cmocka | Sam Lewis 12/06/2017, 10)49
#include "tmp101.h"
float tmp101_get_temperature(void)
{
// Need to set the TMP101 pointer register
to point to the temp register
uint8_t pointer_address = 0;
i2c_transmit_blocking(TMP101_ADDRESS, 0,
&pointer_address, 1);
http://www.samlewis.me/2016/09/embedded-unit-testing-with-cmocka/ Page 2 of 8
Unit testing embedded code with cmocka | Sam Lewis 12/06/2017, 10)49
http://www.samlewis.me/2016/09/embedded-unit-testing-with-cmocka/ Page 3 of 8
Unit testing embedded code with cmocka | Sam Lewis 12/06/2017, 10)49
#ifdef TESTING
return DUMMY_VALUE
#endif
void __wrap_i2c_transmit_blocking(uint8_t
address, uint8_t offset, uint8_t* data, uint8_t
data_size)
{
// allows the calling test to check if the
supplied parameters are as expected
check_expected(address);
check_expected(offset);
}
cmocka cleverly uses the linker to swap out the real function
calls for the mocked ones. To allow for this, mocked out
functions are prefixed with __wrap_. The linker is then
provided with the arguments --wrap=i2c_read_blocking
-Wl,--wrap=i2c_transmit_blocking which allows these
functions to be mocked out. For a complete example of
compiling/linking with cmocka, see the makefile in my
example project.
http://www.samlewis.me/2016/09/embedded-unit-testing-with-cmocka/ Page 4 of 8
Unit testing embedded code with cmocka | Sam Lewis 12/06/2017, 10)49
{
will_return(__wrap_i2c_read_blocking,
0b11100111);
will_return(__wrap_i2c_read_blocking,
0b00000000);
assert_true(tmp101_get_temperature() ==
-25);
}
http://www.samlewis.me/2016/09/embedded-unit-testing-with-cmocka/ Page 5 of 8
Unit testing embedded code with cmocka | Sam Lewis 12/06/2017, 10)49
Back to Home
Comments Community !
1 Login
Sort by Best
$ Recommend 8 Share
http://www.samlewis.me/2016/09/embedded-unit-testing-with-cmocka/ Page 6 of 8
Unit testing embedded code with cmocka | Sam Lewis 12/06/2017, 10)49
code is nice!
http://www.samlewis.me/2016/09/embedded-unit-testing-with-cmocka/ Page 7 of 8
Unit testing embedded code with cmocka | Sam Lewis 12/06/2017, 10)49
http://www.samlewis.me/2016/09/embedded-unit-testing-with-cmocka/ Page 8 of 8