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.

One Reply to “Unit Testing in STM32CubeIDE”

Leave a Reply

Your email address will not be published. Required fields are marked *