From 0944d29597f008f05306f21da6f598f2606ea08e Mon Sep 17 00:00:00 2001 From: Peter Siegmund Date: Fri, 17 May 2024 12:03:16 +0200 Subject: [PATCH] starting with component unit testing (running on device) Signed-off-by: Peter Siegmund --- components/ble/CMakeLists.txt | 3 -- components/logger/CMakeLists.txt | 2 ++ components/logger/include/logger.h | 5 +++ components/logger/logger.c | 11 ++++++ components/logger/test/CMakeLists.txt | 3 ++ components/logger/test/logger_test.c | 7 ++++ main/CMakeLists.txt | 7 ++-- main/main.c | 51 +++++++++++++++++++++++---- {components/ble => main}/osr_ble.c | 0 {components/ble => main}/osr_ble.h | 0 pytest.ini | 1 + test/CMakeLists.txt | 16 +++++++++ test/Makefile | 6 ++++ test/main/CMakeLists.txt | 2 ++ test/main/components_test.c | 16 +++++++++ test/pytest_unittest.py | 6 ++++ test/sdkconfig.defaults | 2 ++ tests/test_main.py | 2 -- 18 files changed, 127 insertions(+), 13 deletions(-) delete mode 100644 components/ble/CMakeLists.txt create mode 100644 components/logger/CMakeLists.txt create mode 100644 components/logger/include/logger.h create mode 100644 components/logger/logger.c create mode 100644 components/logger/test/CMakeLists.txt create mode 100644 components/logger/test/logger_test.c rename {components/ble => main}/osr_ble.c (100%) rename {components/ble => main}/osr_ble.h (100%) create mode 100644 test/CMakeLists.txt create mode 100644 test/Makefile create mode 100644 test/main/CMakeLists.txt create mode 100644 test/main/components_test.c create mode 100644 test/pytest_unittest.py create mode 100644 test/sdkconfig.defaults delete mode 100644 tests/test_main.py diff --git a/components/ble/CMakeLists.txt b/components/ble/CMakeLists.txt deleted file mode 100644 index 18f6170..0000000 --- a/components/ble/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -idf_component_register(SRCS "osr_ble.c" - INCLUDE_DIRS "." - PRIV_REQUIRES "nvs_flash") diff --git a/components/logger/CMakeLists.txt b/components/logger/CMakeLists.txt new file mode 100644 index 0000000..b617ad8 --- /dev/null +++ b/components/logger/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "logger.c" + INCLUDE_DIRS "include") diff --git a/components/logger/include/logger.h b/components/logger/include/logger.h new file mode 100644 index 0000000..6191a90 --- /dev/null +++ b/components/logger/include/logger.h @@ -0,0 +1,5 @@ +#pragma once + +int add(int a, int b); + +void log_message(const char* message); \ No newline at end of file diff --git a/components/logger/logger.c b/components/logger/logger.c new file mode 100644 index 0000000..d86a5b8 --- /dev/null +++ b/components/logger/logger.c @@ -0,0 +1,11 @@ +#include "logger.h" + +#include + +int add(int a, int b) { + return a + b; +} + +void log_message(const char* message) { + printf("%s\n", message); +} diff --git a/components/logger/test/CMakeLists.txt b/components/logger/test/CMakeLists.txt new file mode 100644 index 0000000..f4e1cde --- /dev/null +++ b/components/logger/test/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRC_DIRS "." + INCLUDE_DIRS "." + REQUIRES cmock logger) diff --git a/components/logger/test/logger_test.c b/components/logger/test/logger_test.c new file mode 100644 index 0000000..60d3a9e --- /dev/null +++ b/components/logger/test/logger_test.c @@ -0,0 +1,7 @@ +#include "unity.h" + +#include "logger.h" + +TEST_CASE("Add two numbers", "[logger]") { + TEST_ASSERT_EQUAL(3, add(1, 2)); +} diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index cf2c455..cbe4ded 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,2 +1,5 @@ -idf_component_register(SRCS "main.c" - INCLUDE_DIRS ".") +idf_component_register(SRCS "main.c" "osr_ble.c" + INCLUDE_DIRS "." + PRIV_REQUIRES + nvs_flash + logger) diff --git a/main/main.c b/main/main.c index dbb5d86..abf6e53 100644 --- a/main/main.c +++ b/main/main.c @@ -1,19 +1,58 @@ #include #include - +#include "esp_chip_info.h" +#include "esp_flash.h" +#include "esp_log.h" #include "osr_ble.h" +#include "sdkconfig.h" + +#include "logger.h" + +#define TAG "app" + +void esp32_info() { + /* Print chip information */ + esp_chip_info_t chip_info; + uint32_t flash_size; + esp_chip_info(&chip_info); + printf("This is %s chip with %d CPU core(s), %s%s%s%s, ", CONFIG_IDF_TARGET, + chip_info.cores, + (chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "", + (chip_info.features & CHIP_FEATURE_BT) ? "BT" : "", + (chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "", + (chip_info.features & CHIP_FEATURE_IEEE802154) + ? ", 802.15.4 (Zigbee/Thread)" + : ""); + + unsigned major_rev = chip_info.revision / 100; + unsigned minor_rev = chip_info.revision % 100; + printf("silicon revision v%d.%d, ", major_rev, minor_rev); + if (esp_flash_get_size(NULL, &flash_size) != ESP_OK) { + printf("Get flash size failed"); + return; + } + + printf( + "%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024), + (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external"); + + printf("Minimum free heap size: %" PRIu32 " bytes\n", + esp_get_minimum_free_heap_size()); +} void setup() { osr_ble_init(); + log_message("Hello World!"); } -void loop() { - ESP_ERROR_CHECK(esp_task_wdt_reset()); +void loop(void* args) { + while (1) { + vTaskDelay(pdMS_TO_TICKS(1000)); + ESP_LOGI(TAG, "Hello World!"); + } } void app_main() { setup(); - while (1) { - loop(); - } + xTaskCreatePinnedToCore(loop, "loop", 4096, NULL, 5, NULL, 1); } diff --git a/components/ble/osr_ble.c b/main/osr_ble.c similarity index 100% rename from components/ble/osr_ble.c rename to main/osr_ble.c diff --git a/components/ble/osr_ble.h b/main/osr_ble.h similarity index 100% rename from components/ble/osr_ble.h rename to main/osr_ble.h diff --git a/pytest.ini b/pytest.ini index 0dfe119..2ec057b 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,3 +1,4 @@ [pytest] addopts = --embedded-services esp,idf -s python_files = *.py +timeout = 60 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..3f8fe9a --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,16 @@ +# This is the project CMakeLists.txt file for the test subproject +cmake_minimum_required(VERSION 3.16) + +# Include the components directory of the main application: +# +set(EXTRA_COMPONENT_DIRS "../components") + +# Set the components to include the tests for. +# This can be overriden from CMake cache: +# - when invoking CMake directly: cmake -D TEST_COMPONENTS="xxxxx" .. +# - when using idf.py: idf.py -T xxxxx build +# +set(TEST_COMPONENTS "logger" CACHE STRING "List of components to test") + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(component_unit_test) diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..fd94878 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,6 @@ +test: + @echo "Running tests..." + @idf.py fullclean build + pytest + +.PHONE: test diff --git a/test/main/CMakeLists.txt b/test/main/CMakeLists.txt new file mode 100644 index 0000000..a4b4ea4 --- /dev/null +++ b/test/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "components_test.c" + INCLUDE_DIRS ".") diff --git a/test/main/components_test.c b/test/main/components_test.c new file mode 100644 index 0000000..980e993 --- /dev/null +++ b/test/main/components_test.c @@ -0,0 +1,16 @@ +#include +#include +#include "unity.h" + +static void print_banner(const char* text); + +void app_main(void) { + print_banner("Running all the registered tests"); + UNITY_BEGIN(); + unity_run_all_tests(); + UNITY_END(); +} + +static void print_banner(const char* text) { + printf("\n#### %s #####\n\n", text); +} diff --git a/test/pytest_unittest.py b/test/pytest_unittest.py new file mode 100644 index 0000000..f94b8eb --- /dev/null +++ b/test/pytest_unittest.py @@ -0,0 +1,6 @@ +import pytest +from pytest_embedded import Dut + +def test_unit_test(dut: Dut) -> None: + dut.expect("0 Failures") + dut.expect("Returned from app_main") diff --git a/test/sdkconfig.defaults b/test/sdkconfig.defaults new file mode 100644 index 0000000..4698786 --- /dev/null +++ b/test/sdkconfig.defaults @@ -0,0 +1,2 @@ +# disable watchdog +CONFIG_ESP_TASK_WDT_EN=n diff --git a/tests/test_main.py b/tests/test_main.py deleted file mode 100644 index c293ac5..0000000 --- a/tests/test_main.py +++ /dev/null @@ -1,2 +0,0 @@ -def test_main(dut): - dut.expect('waiting for download')