This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

[UNIT TEST] Unity / CMock - cmock_handle()/native_posix still returns errors

Hello,

I've been working with unit tests lately, and got the example unity project to run (from here). Now I encounter some issues when integrating it into another Zephyr application (currently the blinky example to understand the procedure). As mentioned in the guide, cmock_handle() can be called in CMakeLists.txt to mock zephyr include headers. For instance, the blinky example consists of main.c which includes <zephyr.h>, <device.h>, <devicetree.h> and <drivers/gpio.h>. From my understanding, it is possible to run unit tests on the functions inside main.c when these include headers have been mocked, i.e., 

cmock_handle(${ZEPHYR_BASE}/include/drivers/gpio.h drivers)

However, running the project with native_posix still gives undefined devicetree macros, redefinitions and unknown config variables (CONFIG_GPIO=y and necessary modules are enabled). The mocked header files are included in the test files, where the working unity test example has been used as a base. Initially, CppUTest was used, but as it seemed more work to create the mocks and stubs, Unity / CMock has been used instead. When including CONFIG_GPIO=y, building the project yields an error saying (which may come from native posix board?):

Including boilerplate (Zephyr base): /tmp/ncs/zephyr/cmake/app/boilerplate.cmake
-- Application: /tmp/ncs/projects/project-name
-- Zephyr version: 2.3.0-rc1 (/tmp/ncs/zephyr)
-- Found Python3: /usr/bin/python3.8 (found suitable exact version "3.8.5") found components: Interpreter
-- Board: native_posix
-- Found west: /usr/local/bin/west (found suitable version "0.8.0", minimum required is "0.7.1")
-- Found dtc: /usr/bin/dtc (found suitable version "1.4.7", minimum required is "1.4.6")
-- Found toolchain: host (gcc/ld)
-- Found BOARD.dts: /tmp/ncs/zephyr/boards/posix/native_posix/native_posix.dts
-- Generated zephyr.dts: /tmp/ncs/projects/builds/cmock_test/zephyr/zephyr.dts
-- Generated devicetree_unfixed.h: /tmp/ncs/projects/builds/cmock_test/zephyr/include/generated/devicetree_unfixed.h
Parsing /tmp/ncs/projects/project-name/Kconfig
Loaded configuration '/tmp/ncs/zephyr/boards/posix/native_posix/native_posix_defconfig'
Merged configuration 'unit_test.conf'
Configuration saved to '/tmp/ncs/projects/builds/cmock_test/zephyr/.config'
Kconfig header saved to '/tmp/ncs/projects/builds/cmock_test/zephyr/include/generated/autoconf.h'
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- The ASM compiler identification is GNU
-- Found assembler: /usr/bin/gcc
-- Cache files will be written to: /root/.cache/zephyr
-- Generating test runner /tmp/ncs/projects/builds/cmock_test/runner/runner_example_test.c
-- Generating cmock for header /tmp/ncs/zephyr/include/device.h
-- Configuring done
CMake Error at /tmp/ncs/zephyr/cmake/extensions.cmake:372 (add_library):
  No SOURCES given to target: drivers__gpio
Call Stack (most recent call first):
  /tmp/ncs/zephyr/cmake/extensions.cmake:349 (zephyr_library_named)
  /tmp/ncs/zephyr/drivers/gpio/CMakeLists.txt:3 (zephyr_library)


CMake Generate step failed.  Build files cannot be regenerated correctly.
FATAL ERROR: command exited with status 1: /usr/local/bin/cmake -B/tmp/ncs/projects/builds/cmock_test -S/tmp/ncs/projects/project-name -GNinja -DDoTests=ON

Maybe it depends on the use case? Because the blinky example calls the "device_get_binding()" function and sets a certain pin, which is highly hardware related....

Have I misinterpreted the way of doing this?

Thanks in advance,

EivindTH

Parents
  • Hi Eivind,

      

    Maybe it depends on the use case? Because the blinky example calls the "device_get_binding()" function and sets a certain pin, which is highly hardware related....

    Have I misinterpreted the way of doing this?

    You're on the right track here.

    Currently unity tests can only be build on native_posix target, which means that it cannot be used for unit testing hardware/device tree dependent components.

    We are looking into running unit-tests on qemu_cortex_m3 target, where DT and device driver APIs should be able to be mock'ed.

     

    Kind regards,

    Håkon

Reply
  • Hi Eivind,

      

    Maybe it depends on the use case? Because the blinky example calls the "device_get_binding()" function and sets a certain pin, which is highly hardware related....

    Have I misinterpreted the way of doing this?

    You're on the right track here.

    Currently unity tests can only be build on native_posix target, which means that it cannot be used for unit testing hardware/device tree dependent components.

    We are looking into running unit-tests on qemu_cortex_m3 target, where DT and device driver APIs should be able to be mock'ed.

     

    Kind regards,

    Håkon

Children
  • Hi Håkon,

    Thanks for your answer! I suspected this was the case. Luckily it is still possible to include source code which uses zephyr libraries, and perform unit testing on software algorithmic functionalities, as long as they do not use any hardware/dts components. I guess a workaround would be to perform hardware-in-the-loop tests on a real, physical board.

    By the way, would it be possible to create a custom "device.h" header mock, containing just the required function calls, such as device_get_binding, and additionally define device tree and KConfig variables with predetermined values? Perhaps this would be too much work to do manually..

    Best regards,

    Eivind

Related