tools/bt/safe_unity/README.md
A safe test execution wrapper for the Unity test framework in ESP-IDF projects, designed to prevent test crashes from terminating the entire test suite.
The Safe Unity component provides isolated test execution for Unity framework tests by running each test in a separate child process. This isolation prevents crashes, segmentation faults, or other fatal errors in individual tests from affecting the test runner or other tests.
#include "safe_unity.h"
void test_example_function(void)
{
TEST_ASSERT_EQUAL(42, my_function_that_returns_42());
}
void app_main(void)
{
UNITY_BEGIN();
// Run test safely - crashes won't terminate the test runner
RUN_TEST_SAFE(test_example_function);
UNITY_END();
}
RUN_TEST_SAFE(func)Safely executes a Unity test function in an isolated process.
Parameters:
func: Unity test function to executeExample:
RUN_TEST_SAFE(my_test_function);
fork()The component handles the following signals in child processes:
SIGSEGV: Segmentation faultSIGABRT: Abort signalSIGFPE: Floating point exceptionSIGILL: Illegal instructionSIGBUS: Bus errorWhen any of these signals are received, the test is marked as crashed and the process exits gracefully after flushing coverage data.
The component automatically integrates with gcov for code coverage collection:
--coverage flagsThe component requires the following CMake configuration:
# In your project's CMakeLists.txt
if(${target} STREQUAL "linux")
idf_component_register(
SRCS "your_test.c"
INCLUDE_DIRS "."
REQUIRES safe_unity
)
endif()
#include "safe_unity.h"
void setUp(void) {
// Test setup code
}
void tearDown(void) {
// Test cleanup code
}
void test_normal_operation(void) {
TEST_ASSERT_EQUAL(42, my_function());
}
void test_edge_case(void) {
TEST_ASSERT_NULL(my_function_with_null_return());
}
void test_potential_crash(void) {
// This might crash in some conditions
my_risky_function();
TEST_ASSERT_TRUE(true);
}
void app_main(void) {
UNITY_BEGIN();
RUN_TEST_SAFE(test_normal_operation);
RUN_TEST_SAFE(test_edge_case);
RUN_TEST_SAFE(test_potential_crash);
UNITY_END();
}