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

Parents Reply Children
  • Hi,

    This is just a short update. I am still waiting for reply regarding your issue. I will get back to you as soon as I get some relevant information.

    Best regards,
    Dejan

  • Just an quick update. It does not seem like our developers are any experience with using C++ and unity/Cmock with NCS. As Unity was written for C I would suggest taking a look at Ztest since it also have mock support. Cmock is part of the same package as Unity and is also as far as I know primary developed to be used together with C. You can also use FFF for mocking. 

    For embedded we primarily use C at Nordic (Matter excluded) so C++ is not what we dabble with the most. As a personal opinion I'm not that found of using Unity/Cmock without using Ceedling as well, but from my experience in the past, I'm not to sure how user friendly it will be  trying to sett up Ceedling  together with Zephyr. 

    Regards

    Runar

  • 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