Search code examples
c++unit-testingarduinoesp32platformio

How to run a Unit-Test for ESP32 without flashing it?


Knowing from other languages and platforms we want to use Unit-Testing during the Build-Process BEFORE the code is flashed to the hardware. This should be possible for simple functions tests which have no need for the ESP32 hardware.

But as we understand yet the C++ code is compiled (and linked) for the ESP32 chip and shall not run on the developing system or in a CI/CD pipeline.

Is there any way to emulate ESP32 (for C++) or run Unit-Tests on any other way on another systems?


note: We are using 'platformio' for the build.


Solution

  • It is possible to do this with the qemu_esp32 emulator. You can compile your test runner and run it directly on the emulator instead of flashing it to a real esp32 chip.

    Here is an example of how to do this (adapted from esp32_qemu_unity_test_action):

    1. Compile the test runner like you normally would. Before you proceed you might want to actually flash this just to know your test runner and tests are working.
    idf.py build
    cd build 
    
    1. Normally when you flash a esp32, you're writing several binary files to specific locations. This command merges all those into a single full binary image.
    esptool.py --chip esp32 merge_bin --fill-flash-size 4MB -o flash_image.bin @flash_args
    
    1. Run the esp32 emulator and provide the full binary flash image.
    /opt/qemu/bin/qemu-system-xtensa -nographic -no-reboot -machine esp32 -drive file=flash_image.bin,if=mtd,format=raw -serial file:output.log
    

    By specifying -no-reboot, the emulator will simply exit instead of rebooting.