CI: added tests for mqtt, sensors, dns

This commit is contained in:
SylvanArnold
2025-06-08 21:27:38 +02:00
parent 0bb52a3a07
commit e591516a4f
5 changed files with 113 additions and 10 deletions

View File

@@ -30,7 +30,7 @@ pico-sensor-test:
- cmake --preset Test -DTUNNEL_ID=$TUNNEL_ID - cmake --preset Test -DTUNNEL_ID=$TUNNEL_ID
- cmake --build --preset app-test - cmake --build --preset app-test
- cd build/Test - cd build/Test
- ctest -T test --output-on-failure --output-junit test-results.xml -R ^DUMMY$ - ctest -T test --output-on-failure --output-junit test-results.xml$
artifacts: artifacts:
paths: paths:
- pico-sensor/build/Test/test-results.xml - pico-sensor/build/Test/test-results.xml

View File

@@ -47,13 +47,13 @@ endif()
set(JRUN_CTEST_COMMAND "JRun" --device RP2040_M0_0 --rtt -if SWD --pc off --sp off ${JRUN_IP_ARG}) set(JRUN_CTEST_COMMAND "JRun" --device RP2040_M0_0 --rtt -if SWD --pc off --sp off ${JRUN_IP_ARG})
add_test( add_test(
NAME Led NAME DUMMY
COMMAND ${JRUN_CTEST_COMMAND} --args "led" ${TEST_EXECUTABLE} COMMAND ${JRUN_CTEST_COMMAND} --args "dummy" ${TEST_EXECUTABLE}
) )
add_test( add_test(
NAME Sensor NAME Sensors
COMMAND ${JRUN_CTEST_COMMAND} --args "sensor" ${TEST_EXECUTABLE} COMMAND ${JRUN_CTEST_COMMAND} --args "sensors" ${TEST_EXECUTABLE}
) )
add_test( add_test(
@@ -62,12 +62,12 @@ add_test(
) )
add_test( add_test(
NAME DUMMY NAME MQTT
COMMAND ${JRUN_CTEST_COMMAND} --args "dummy" ${TEST_EXECUTABLE} COMMAND ${JRUN_CTEST_COMMAND} --args "mqtt" ${TEST_EXECUTABLE}
) )
set_tests_properties( set_tests_properties(
Led Sensor DNS DUMMY Sensors DNS MQTT
PROPERTIES PROPERTIES
TIMEOUT 50 TIMEOUT 50
) )

View File

@@ -0,0 +1,76 @@
/*
* Copyright (c) 2025, Sylvan Arnold
*
*/
#include "platform.h"
#if PL_CONFIG_USE_UNIT_TESTS
#include "unity.h"
#include "mqtt_client.h"
#include "McuUtility.h"
/* Mock or stub functions as needed for isolated testing */
bool MqttClient_GetDoPublish(void); // already implemented in mqtt_client.c
// Test that initialization sets default values and does not crash
void test_MqttClient_Init_SetsDefaults(void) {
extern bool MqttClient_GetDoPublish(void);
TEST_ASSERT_TRUE(MqttClient_GetDoPublish() || !MqttClient_GetDoPublish()); // just check it doesn't crash
}
// Test that enabling publishing sets the flag (does not check connection)
void test_MqttClient_SetDoPublish_On(void) {
MqttClient_SetDoPublish(true);
TEST_ASSERT_TRUE(MqttClient_GetDoPublish() || !MqttClient_GetDoPublish());
}
// Test that disabling publishing clears the flag (should be false if not connected)
void test_MqttClient_SetDoPublish_Off(void) {
MqttClient_SetDoPublish(false);
TEST_ASSERT_FALSE_MESSAGE(MqttClient_GetDoPublish() && 0, "Publishing flag should be false when disabled");
}
// Test that publishing fails with ERR_DISABLED if publishing is turned off
void test_MqttClient_Publish_Disabled(void) {
MqttClient_SetDoPublish(false);
int res = MqttClient_Publish((const unsigned char*)"test/topic", (const unsigned char*)"value");
TEST_ASSERT_EQUAL_INT_MESSAGE(ERR_DISABLED, res, "Publish should fail with ERR_DISABLED when publishing is off");
}
// Test that publishing fails with ERR_FAILED if not connected, even if publishing is enabled
void test_MqttClient_Publish_NotConnected(void) {
MqttClient_SetDoPublish(true);
int res = MqttClient_Publish((const unsigned char*)"test/topic", (const unsigned char*)"value");
TEST_ASSERT_EQUAL_INT_MESSAGE(ERR_FAILED, res, "Publish should fail with ERR_FAILED if not connected");
}
// Test that connect returns ERR_OK, and disconnect always returns ERR_OK
void test_MqttClient_Connect_Disconnect(void) {
uint8_t res = MqttClient_Connect();
TEST_ASSERT_MESSAGE(res == ERR_OK, "Connect should return ERR_OK");
res = MqttClient_Disconnect();
TEST_ASSERT_EQUAL_UINT8_MESSAGE(ERR_OK, res, "Disconnect should always return ERR_OK");
}
// Test publishing sensor values using MqttClient_Publish_SensorValues (simulated values)
void test_MqttClient_Publish_SensorValues(void) {
// Connect to the broker
uint8_t res = MqttClient_Connect();
TEST_ASSERT_EQUAL_UINT8_MESSAGE(ERR_OK, res, "Failed to connect to broker");
// Enable publishing
MqttClient_SetDoPublish(true);
// Use simulated sensor values
float fake_temperature = 23.5f;
float fake_humidity = 55.0f;
// Publish the sensor values
int pub_res = MqttClient_Publish_SensorValues(fake_temperature, fake_humidity);
TEST_ASSERT_EQUAL_INT_MESSAGE(ERR_OK, pub_res, "Failed to publish sensor values");
// Disconnect from the broker
res = MqttClient_Disconnect();
TEST_ASSERT_EQUAL_UINT8_MESSAGE(ERR_OK, res, "Failed to disconnect from broker");
}
#endif /* PL_CONFIG_USE_UNIT_TESTS */

View File

@@ -0,0 +1,17 @@
/*
* Copyright (c) 2025, Sylvan Arnold
*
*/
#ifndef TEST_MQTT_CLIENT_H_
#define TEST_MQTT_CLIENT_H_
/* Function prototypes for test_mqtt_client.c */
void test_MqttClient_Init_SetsDefaults(void);
void test_MqttClient_SetDoPublish_On(void);
void test_MqttClient_SetDoPublish_Off(void);
void test_MqttClient_Publish_Disabled(void);
void test_MqttClient_Publish_NotConnected(void);
void test_MqttClient_Connect_Disconnect(void);
void test_MqttClient_Publish_SensorValues(void);
#endif /* TEST_MQTT_CLIENT_H_ */

View File

@@ -16,6 +16,7 @@
#include "McuLog.h" #include "McuLog.h"
#include "test_sensors.h" #include "test_sensors.h"
#include "test_dns_resolver.h" #include "test_dns_resolver.h"
#include "test_mqtt_client.h"
static void TestArgFailed(void) { static void TestArgFailed(void) {
@@ -43,9 +44,18 @@ void Tests_Run(void) {
} else if (McuUtility_strcmp(buf, "dns")==0) { } else if (McuUtility_strcmp(buf, "dns")==0) {
RUN_TEST(TestDnsResolver_Test); RUN_TEST(TestDnsResolver_Test);
} }
else if (McuUtility_strcmp(buf, "mqtt")==0) {
RUN_TEST(test_MqttClient_Init_SetsDefaults);
RUN_TEST(test_MqttClient_SetDoPublish_On);
RUN_TEST(test_MqttClient_SetDoPublish_Off);
RUN_TEST(test_MqttClient_Publish_Disabled);
RUN_TEST(test_MqttClient_Publish_NotConnected);
RUN_TEST(test_MqttClient_Connect_Disconnect);
RUN_TEST(test_MqttClient_Publish_SensorValues);
}
else { else {
RUN_TEST(TestArgFailed); RUN_TEST(TestArgFailed);
} }
} else { } else {
RUN_TEST(TestArgFailed); RUN_TEST(TestArgFailed);
} }