Unit Testing in STM32CubeIDE

Test-driven development is becoming a trend in software development, actually it is a trend in software development that deals with high level languages. Now, it is also getting popular among embedded software development. So, in this post how to integrate Cpputest (http://cpputest.github.io) to STM32CubeIDE will be explained. There are two steps:

  1. Creating and compiling static library project from cpputest’s sources
  2. Creating and testing STM32Cube project with library.

Static Library

Follow the steps below:

  • Choose File->New-> STM32 Project
  • Pick the MCU, STM32F407VG in my case, click Next
    • Targeted Language: C++
    • Targeted Binary Type: Static Library
    • Targeted Project Type: Empty
    • Finish
  • Clone Cpputest from https://github.com/cpputest/cpputest
  • From the cloned cpputest-master directory copy all source files from src/CppUTest and src/CppUTestExt to your project’s src folder
  • Copy src/Platforms/Gcc/UtestPlatform.cpp from the master directory to your project’s src folder
  • Project->Properties -> C/C++ Build-> Settings-> -> Tool Settings ->MCU G++ Compiler -> Include paths -> add cpputest-master/include
  • Project->Properties->C/C++ Build-> Settings-> -> Tool Settings ->MCU G++ Compiler -> Preprocessor -> add CPPUTEST_STD_CPP_LIB_DISABLED
  • Project->Properties->C/C++ Build-> Settings-> -> Tool Settings ->MCU G++ Compiler -> Optimization -> Check
    • Disable handling exceptions
    • Disable generation of information about every class with virtual functions
  • At IEEE754ExceptionsPlugin.cpp change #ifdef CPPUTEST_HAVE_FENV at Line 31 to #ifdef CPPUTEST_HAVE_FENV_, so #else part will compile
  • Build Project

STM32Cube Project

Follow the steps below:

  • Choose File->New-> STM32 Project
  • Pick the MCU, STM32F407VG in my case, click Next
    • Targeted Language: C++
    • Targeted Binary Type: Executable
    • Targeted Project Type: STM32Cube
    • Finish
  • Project->Properties->C/C++ Build-> Settings-> -> Tool Settings ->MCU G++ Compiler -> Preprocessor -> add
    • CPPUTEST_STD_CPP_LIB_DISABLED
    • CPPUTEST_STD_C_LIB_DISABLED
  • Project->Properties -> C/C++ Build-> Settings-> -> Tool Settings ->MCU G++ Compiler -> Include paths -> add cpputest-master/include
  • Project->Properties->C/C++ Build-> Settings-> -> Tool Settings ->MCU G++ Compiler -> Optimization -> Check
    • Disable handling exceptions
    • Disable generation of information about every class with virtual functions
  • Project->Properties->C/C++ Build-> Settings-> -> Tool Settings ->MCU G++ Linker -> Libraries ->
    • Library Search Path -> add Debug folder of the compiled static library project
    • Libraries -> add compiled static library’s project name
  • After these project settings now it’s time to write some code
  • Rename main.c as main.cpp, then
  • Perform the changes in the following snippet
#include "CppUTest/CommandLineTestRunner.h" // add the header file

const char * av_override[] = { }; // local variable in main()
CommandLineTestRunner::RunAllTests(0, av_override); // call after peripheral initializations
  • Add another source file “test_src.cpp” that includes test functions, add following snippet to it
#include "CppUTest/TestHarness.h"
TEST_GROUP(FirstTestGroup)
{
};
TEST(FirstTestGroup, FirstTest) // failure
{
    FAIL("Embedded Area");
}

TEST(FirstTestGroup, SecondTest) // successful
{
    CHECK_EQUAL_ZERO(0);
}

Finally follow the steps from this post and add SWV to your project. Test results will be seen on the SWV ITM Data Console while debugging.

5 Replies to “Unit Testing in STM32CubeIDE”

    1. It’s actually related to project’s needs. Since cpputest is used as plug-and-play tool, we assume that there is no need to change the cpputest source code. It makes easier to deploy and use in other projects as well.

  1. So this post is about creating the test project. However it is not clear what the project is that will be the subject for testing.
    In addition, for my case, our project is on C rather than C++.
    Could you include a full case including the test subject as well as the tests.

    Thanks

  2. I also want to implement the unity testing in STM32 Cube IDE, but my code is written in C instead of C++. Do you any resource on github for the same??
    Can I follow the same above procedure for my implementation??
    Any help would be really helpful!

Leave a Reply

Your email address will not be published.