4

I am developing firmware for stm32f4xx based systems. For that I setup a toolchain based on the arm-none-eabi-gcc toolchain form ARM and cmake. This toolchain works on Ubuntu. I can x-compile and debug(via openocd + eclipse IDE). Now I like to add do some functional testing for my code. I was checking and it seems that cmocka is a good tool to use for embedded software testing.

I am now looking for an example/template that integrates a test into the cmake build.

let's assume a simple function at myfunc.c

#include "myFunc.h"

int buffer[10];

void myFunc(int i, int val) {
    buffer[i] = val;
}

if I got it right I can do a test in a separate c file like "test.c"

#include "myFunc.h"
#include <cmocka.h>

// function success
static void test_myFunc_positive() {
    for(int i = 0; i < 10; i++) {
        myFunc(i,i);
    }
}

static void test_myFunc_outofbounds() {
    myFunc(100,44);
}


int main(void) {
    const struct CMUnitTest tests[] = {
        cmocka_unit_test(test_myFunc_positive),
        cmocka_unit_test(test_myFunc_outofbounds),
    };
    return cmocka_run_group_tests(tests, NULL, NULL);
}

Usually I run

cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE="cmake/STM32Toolchain.cmake"

My question contains some sub questions: 1.) I installed libcmocka-dev. This is for my host system. Do I need to install cmocka for my arm-none-eabi-gcc compiler? 2.) How to setup cmake to pick the cmocka lib, build the test and run it on the host system? Think my toolchain file needs to be ignored.

Stefan Jaritz
  • 1,999
  • 7
  • 36
  • 60
  • 1
    I think not necessary for target to run cmoka, we usually write a.c and a corresponding a_test.c with cmoka, and this unit test only run on host – How Chen Jun 15 '18 at 17:40
  • 1
    Youre right. It is not necessary. But often it is hard to separate the test code to make it compilable on the host. So on target testing is a serious scenario. – eDeviser Oct 23 '18 at 07:06
  • Any luck on this front? I, too, have a project with Cmake, arm-none-eabi-gcc, and STM32f4xx based system. I tried simply adding cmocka as a sub-directory and adding it to my cmake file, but seems to get stuck thinking it's compiling for Linux with Linux-like features. – CivFan Dec 05 '22 at 21:37

1 Answers1

1

Your source code looks pretty fine. Here is a recipe of how you could use cmocka. I recommend to crosscompile cmocka's source code, too. In fact I doing it in this way:

  1. Add cmocka.c to your sources
  2. Add 'cmocka.h and cmocka_pbc.h and cmocka_private.h to your include directories.
  3. Compile and run your software

PS: I don't know libcmocka-dev. I think this is a precompiled version of cmocka?

PPS: I had some trouble on getting cmocka's output redirected to my serial UART. If you're having the same problems, feel free to ask.

Community
  • 1
  • 1
eDeviser
  • 1,605
  • 2
  • 17
  • 44
  • You probably also need to add `--specs=nosys.specs` to the compiler and linker flags. I also had to add `-DHAVE_CLOCK_GETTIME=0` and `-DHAVE_CLOCK_GETTIME_REALTIME=0` to my `cmake ..` command, so that it wouldn't try to use the function `clock_getttime()`. – CivFan Dec 13 '22 at 23:40