I have a project with a few components that I want to test. I've read the documentation and started to create unit tests using unity for one of my component (the component is switches.hpp/cpp). I've created a test folder in the component's folder and added a CMakeLists.txt and the test file as .cpp (since my project is in C++.
switch/test/CMakeLists.txt
Code: Select all
idf_component_register(SRC "test_switches.cpp"
INCLUDE_DIRS "."
REQUIRES unity switches)
Code: Select all
#include "unity.h"
#include "driver/gpio.h"
#include "switches.hpp"
Switches* switches = nullptr;
void setUp(void) {
switches = Switches::get_instance();
switches->set_switch_m1_state(SWITCH_M1_A);
switches->set_switch_m2_state(SWITCH_M2_D);
switches->set_switch_m3_state(SWITCH_M3_D);
switches->set_switch_m4_state(SWITCH_M4_GO);
switches->set_switch_m5_state(SWITCH_M5_F);
}
void tearDown(void) {
// TODO
}
void test_switch_m1(void) {
switches->set_switch_m1_state(SWITCH_M1_B);
TEST_ASSERT_EQUAL(SWITCH_M1_B, switches->get_switch_m1_state());
//TEST_ASSERT_EQUAL(1, gpio_get_level(SWITCH_M1_PIN1));
//TEST_ASSERT_EQUAL(0, gpio_get_level(SWITCH_M1_PIN2));
}
extern "C" void app_main(void) {
UNITY_BEGIN();
RUN_TEST(test_switch_m1);
UNITY_END();
}
unity-app/main/test_app_main.c
Code: Select all
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "unity.h"
static void print_banner(const char* text)
{
printf("\n#### %s #####\n\n", text);
}
void app_main(void)
{
print_banner("Unity test runner");
printf("Total test count: %d\n", unity_get_test_count());
UNITY_BEGIN();
unity_run_all_tests();
UNITY_END();
unity_run_menu();
}
Code: Select all
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/version.cmake)
# Add newly added components to one of these lines:
# 1. Add here if the component is compatible with IDF >= v4.3
set(EXTRA_COMPONENT_DIRS "../components")
set(TEST_COMPONENTS switches)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(test_app_main)