Unit testing c++ NCS code

Hi,

Im currently using Cmock and Unity like described here: https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/test_and_optimize/testing_unity_cmock.html

the difference is that my usercode is written in c++, which means that I will need to instantiate classes and use strings, vectors etc. in my tests.

I got everything working up until the point where I get undefined reference to `test_suiteTearDown(int)'. when I wrap my code in extern "C"{...} I will get undefined reference to `unity_main'. I think this is due to the unity functions now being compiled with c++ linkage.

this seems to be just a tiny thing that stops me from using this setup for c++ testing but I dont know how to solve it. Any ideas?

I also tried to use Ztest with Cmock instead, but this doesnt work because NCS cmock implementation seems to depend on unity.

I would love if anyone could share some ideas on how to solve this problem.

Kind regards,

Jonas

  • ok thanks for the info! I know FFF and Ztest exists and I tried using it, but the Problem is that it does not support dependecy injection. I dont know how I should mock Zephyr functions without modifying my source file and including #ifdef TEST everywhere to include the mocked libraries instead of the real zephyr ones. Also I would have to write mocking functions for all the zephyr functions by myself which is quite a task.

  • I got back the following from one of the developers. Most of the ones I talked to had only used it for C, and the ones who had tried with C++ had run into trouble. 

    "

    I think that in the customer case they might have wrapped too much code with extern "C". If a test case is a cpp file, then the unity framework will generate build/runner/runner_<test_case_filename>, so another cpp file. And that file will contain unity_main definition among others. If the customer wraps extern int unity_main(void) with extern "C" then the symbol is not found because the generated runner uses C++ linkage.

     

    So, let them only wrap necessary functions with extern "C" (functions called by unity) and it will build fine."
    regards

    Runar

Related