Page 1 of 1

Unit testing ESP-IDF C++ components with GoogleTest (host-based)

Posted: Sun Feb 22, 2026 6:25 am
by aluiziotomazelli
I've been using Unity for a while but started migrating to GoogleTest mainly because of GMock. When your component has multiple C++ classes, writing mocks by hand gets painful fast — GMock handles it in a few lines directly in the test file.

The main challenge was integrating GTest into the IDF build system. The solution I came up with was a small wrapper component that uses FetchContent to pull GTest from GitHub at build time, restricted to the linux target only, so it never touches the ESP32 binary. The two-phase IDF build also needs a guard (NOT CMAKE_BUILD_EARLY_EXPANSION) to prevent FetchContent from running during dependency scanning.

I put together a repo documenting the setup: https://github.com/aluiziotomazelli/gtest-esp-idf

The first example (01_basic_test) is intentionally simple — just a Sum class with basic arithmetic and constrained addition. The goal is to validate the integration, not the logic. Next chapters may cover GMock and more complex scenarios.

Code: Select all

cd 01_basic_test/host_test/test_sum
idf.py --preview set-target linux
idf.py build
./build/test_sum.elf
I am using ESP-IDF v5.5.1, but ESP-IDF Docker container (idf-env latest) will work, CI actions on the repo run exactly that way.

Feedback welcome, especially if anyone has a cleaner way to handle the wrapper.