How to setup a cmocka example with arm-none-eabi-gcc + cmake?

2.1k Views Asked by At

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.

1

There are 1 best solutions below

1
eDeviser On

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.