Gtest Setup for Embedded Software
Gtest Setup for Embedded Software
Project structure
Test runner
Report generation
Sample output
1. PROJECT STRUCTURE
firmware_project/
├── CMakeLists.txt
├── src/
│ ├── pcm/
│ │ ├── pcm.c
│ │ └── pcm.h
│ ├── tcm/
│ │ └── ...
│ └── fcm/
│ └── ...
├── tests/
│ ├── CMakeLists.txt
│ ├── main.cpp # GTest runner
│ ├── pcm/
│ │ └── test_pcm.cpp
│ └── ...
└── scripts/
└── run_tests.sh # Automation script
src/pcm/pcm.h
#ifndef PCM_H
#define PCM_H
#endif
src/pcm/pcm.c
#include "pcm.h"
extern "C" {
#include "../../src/pcm/pcm.h"
}
TEST(PCMTests, AddFunction) {
EXPECT_EQ(pcm_add(2, 3), 5);
}
5. CMAKE CONFIGURATION
CMakeLists.txt (Root)
cmake_minimum_required(VERSION 3.10)
project(FirmwareTest C)
set(CMAKE_C_STANDARD 11)
# Add subdirectories
add_subdirectory(src)
add_subdirectory(tests)
src/CMakeLists.txt
add_library(pcm pcm/pcm.c)
# You can do the same for tcm.c and fcm.c if needed
tests/CMakeLists.txt
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR}/src)
add_executable(runTests
main.cpp
pcm/test_pcm.cpp
)
target_link_libraries(runTests
GTest::GTest
GTest::Main
pcm
)
junit-viewer (HTML)
xsltproc + junit-xml.xsl
7. AUTOMATION SCRIPT
scripts/run_tests.sh
#!/bin/bash
set -e
BUILD_DIR=build
# Build project
cmake ..
make
# Convert to HTML
junit-viewer --results=test_results.xml --save test_report.html
8. RUNNING EVERYTHING
./scripts/run_tests.sh
if(CODE_COVERAGE)
message(STATUS "Code coverage enabled")
add_compile_options(-g -O0 --coverage)
link_libraries(gcov)
endif()
tests/CMakeLists.txt
set -e
BUILD_DIR=build
# Clean build
rm -rf $BUILD_DIR
mkdir -p $BUILD_DIR
cd $BUILD_DIR
# Run tests
./runTests --gtest_output=xml:test_results.xml
7. Final Command
To run everything:
./scripts/run_tests.sh