From 4551507f63faa6be47fcc849f6904a52cbfd6a1a Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Thu, 27 Nov 2025 16:23:00 +0530 Subject: [PATCH] examples/unit_test_app: added an unit test app --- examples/.build-rules.yml | 7 +++ examples/unit_test_app/CMakeLists.txt | 23 ++++++++++ examples/unit_test_app/README.md | 52 ++++++++++++++++++++++ examples/unit_test_app/main/CMakeLists.txt | 3 ++ examples/unit_test_app/main/app_main.c | 16 +++++++ examples/unit_test_app/partitions.csv | 10 +++++ examples/unit_test_app/sdkconfig.defaults | 52 ++++++++++++++++++++++ 7 files changed, 163 insertions(+) create mode 100644 examples/unit_test_app/CMakeLists.txt create mode 100644 examples/unit_test_app/README.md create mode 100644 examples/unit_test_app/main/CMakeLists.txt create mode 100644 examples/unit_test_app/main/app_main.c create mode 100644 examples/unit_test_app/partitions.csv create mode 100644 examples/unit_test_app/sdkconfig.defaults diff --git a/examples/.build-rules.yml b/examples/.build-rules.yml index 2f6047544..879355005 100644 --- a/examples/.build-rules.yml +++ b/examples/.build-rules.yml @@ -134,8 +134,15 @@ examples/bridge_apps/esp_rainmaker_bridge: - if: IDF_TARGET in ["esp32s3"] temporary: true reason: the other targets are not tested yet + examples/camera: enable: - if: IDF_TARGET in [""] temporary: true reason: Another CI has been added + +examples/unit_test_app: + enable: + - if: IDF_TARGET in ["esp32c3"] + temporary: true + reason: the other targets are not tested yet diff --git a/examples/unit_test_app/CMakeLists.txt b/examples/unit_test_app/CMakeLists.txt new file mode 100644 index 000000000..fcf32bb98 --- /dev/null +++ b/examples/unit_test_app/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.5) + +set(PROJECT_VER "1.0") +set(PROJECT_VER_NUMBER 1) + +set(ESP_MATTER_PATH $ENV{ESP_MATTER_PATH}) +set(MATTER_SDK_PATH ${ESP_MATTER_PATH}/connectedhomeip/connectedhomeip) + +set(EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_LIST_DIR}/../../components" + "${MATTER_SDK_PATH}/config/esp32/components") + +# Set the components to include the tests for. +set(TEST_COMPONENTS "esp_matter" CACHE STRING "List of components to test") + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(unit_test_app) + +# TODO: Remove -Wno-error=unused-result once submodules are updated to not treat unused return values as errors. +idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++17;-Os;-DCHIP_HAVE_CONFIG_H;-Wno-overloaded-virtual;-Wno-error=unused-result" APPEND) +idf_build_set_property(C_COMPILE_OPTIONS "-Os" APPEND) +# For RISCV chips, project_include.cmake sets -Wno-format, but does not clear various +# flags that depend on -Wformat +idf_build_set_property(COMPILE_OPTIONS "-Wno-format-nonliteral;-Wno-format-security" APPEND) diff --git a/examples/unit_test_app/README.md b/examples/unit_test_app/README.md new file mode 100644 index 000000000..fdc4da332 --- /dev/null +++ b/examples/unit_test_app/README.md @@ -0,0 +1,52 @@ +# ESP Matter Unit Test App + +This application runs unit tests for the ESP Matter component using the Unity test framework. + +## Unity Test Framework + +This test app uses the Unity Test Framework suggested by ESP-IDF. + +Further reads: +- [Unit Testing with Unity](https://docs.espressif.com/projects/vscode-esp-idf-extension/en/latest/additionalfeatures/unit-testing.html) +- [Unit Testing in ESP32](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/unit-tests.html) + +## Running the Tests + +1. Build the application: +```bash +cd examples/unit_test_app +idf.py build +``` + +2. Flash to device: +```bash +idf.py -p flash monitor +``` + +3. Run tests: +Once flashed, the test menu will appear in the serial monitor. You can: +- Press `Enter` to see the list of available tests +- Enter a test number to run a specific test +- Enter `*` to run all tests + +## Extending the Tests + +### Adding tests to existing component + +1. Create a new `.cpp` file in `components//test/` +2. Add the filename to the source list in `components//test/CMakeLists.txt`: +```cmake +list(APPEND srcs_list "your_new_test_file.cpp") +``` +3. Write the test cases in the new test file. + +### Adding new component tests + +Please refer to components/esp_matter/test directory for comprehensive structure and example. + +- After adding the new component tests, you need to add the component to the TEST_COMPONENTS list in CMakeLists.txt +- Append the component name to the TEST_COMPONENTS list. For example, if you add a new component called "new_component", you need to add it to the TEST_COMPONENTS list in CMakeLists.txt: + +```cmake +set(TEST_COMPONENTS "esp_matter new_component" CACHE STRING "List of components to test") +``` \ No newline at end of file diff --git a/examples/unit_test_app/main/CMakeLists.txt b/examples/unit_test_app/main/CMakeLists.txt new file mode 100644 index 000000000..9d7ee8c59 --- /dev/null +++ b/examples/unit_test_app/main/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "app_main.c" + INCLUDE_DIRS "." + REQUIRES unity esp_matter) diff --git a/examples/unit_test_app/main/app_main.c b/examples/unit_test_app/main/app_main.c new file mode 100644 index 000000000..063c2cd58 --- /dev/null +++ b/examples/unit_test_app/main/app_main.c @@ -0,0 +1,16 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include "unity.h" +#include "esp_log.h" + +static const char *TAG = "UT"; + +void app_main(void) +{ + ESP_LOGI(TAG, "esp-matter unit test app"); + unity_run_menu(); +} \ No newline at end of file diff --git a/examples/unit_test_app/partitions.csv b/examples/unit_test_app/partitions.csv new file mode 100644 index 000000000..ffe5f242e --- /dev/null +++ b/examples/unit_test_app/partitions.csv @@ -0,0 +1,10 @@ +# Name, Type, SubType, Offset, Size, Flags +# Note: Firmware partition offset needs to be 64K aligned, initial 36K (9 sectors) are reserved for bootloader and partition table +esp_secure_cert, 0x3F, ,0xd000, 0x2000, encrypted +nvs, data, nvs, 0x10000, 0xC000, +nvs_keys, data, nvs_keys,, 0x1000, encrypted +otadata, data, ota, , 0x2000 +phy_init, data, phy, , 0x1000, +ota_0, app, ota_0, 0x20000, 0x1E0000, +ota_1, app, ota_1, 0x200000, 0x1E0000, +fctry, data, nvs, 0x3E0000, 0x6000 diff --git a/examples/unit_test_app/sdkconfig.defaults b/examples/unit_test_app/sdkconfig.defaults new file mode 100644 index 000000000..9247aeb21 --- /dev/null +++ b/examples/unit_test_app/sdkconfig.defaults @@ -0,0 +1,52 @@ +# Unity Framework Configuration +CONFIG_UNITY_ENABLE_FLOAT=y +CONFIG_UNITY_ENABLE_DOUBLE=y +CONFIG_UNITY_ENABLE_64BIT=y + +CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y + +#enable BT +CONFIG_BT_ENABLED=n +#enable lwip ipv6 autoconfig +CONFIG_LWIP_IPV6_AUTOCONFIG=y + +# Use a custom partition table +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" +CONFIG_PARTITION_TABLE_OFFSET=0xC000 + +# Enable chip shell +CONFIG_ENABLE_CHIP_SHELL=y + +# Disable WiFi AP +CONFIG_ENABLE_WIFI_AP=n +CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n + +#enable lwIP route hooks +CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT=y +CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=y + +# This example only use 2 dynamic endpoints +CONFIG_ESP_MATTER_MAX_DYNAMIC_ENDPOINT_COUNT=16 + +# Enable HKDF in mbedtls +CONFIG_MBEDTLS_HKDF_C=y + +# Increase LwIP IPv6 address number to 6 (MAX_FABRIC + 1) +# unique local addresses for fabrics(MAX_FABRIC), a link local address(1) +CONFIG_LWIP_IPV6_NUM_ADDRESSES=6 + +# borrowed from unit-test-app +CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK=y +CONFIG_HEAP_POISONING_COMPREHENSIVE=y +CONFIG_SPI_FLASH_ENABLE_COUNTERS=y +CONFIG_ESP_TASK_WDT_INIT=n +CONFIG_SPI_FLASH_DANGEROUS_WRITE_FAILS=y +CONFIG_COMPILER_STACK_CHECK_MODE_STRONG=y +CONFIG_COMPILER_STACK_CHECK=y +CONFIG_ADC_DISABLE_DAC=n +CONFIG_COMPILER_WARN_WRITE_STRINGS=y +CONFIG_SPI_MASTER_IN_IRAM=y +CONFIG_EFUSE_VIRTUAL=y +CONFIG_UNITY_ENABLE_BACKTRACE_ON_FAIL=y +