CI: added tests for mqtt, sensors, dns
This commit is contained in:
@@ -47,13 +47,13 @@ endif()
|
||||
set(JRUN_CTEST_COMMAND "JRun" --device RP2040_M0_0 --rtt -if SWD --pc off --sp off ${JRUN_IP_ARG})
|
||||
|
||||
add_test(
|
||||
NAME Led
|
||||
COMMAND ${JRUN_CTEST_COMMAND} --args "led" ${TEST_EXECUTABLE}
|
||||
NAME DUMMY
|
||||
COMMAND ${JRUN_CTEST_COMMAND} --args "dummy" ${TEST_EXECUTABLE}
|
||||
)
|
||||
|
||||
add_test(
|
||||
NAME Sensor
|
||||
COMMAND ${JRUN_CTEST_COMMAND} --args "sensor" ${TEST_EXECUTABLE}
|
||||
NAME Sensors
|
||||
COMMAND ${JRUN_CTEST_COMMAND} --args "sensors" ${TEST_EXECUTABLE}
|
||||
)
|
||||
|
||||
add_test(
|
||||
@@ -62,12 +62,12 @@ add_test(
|
||||
)
|
||||
|
||||
add_test(
|
||||
NAME DUMMY
|
||||
COMMAND ${JRUN_CTEST_COMMAND} --args "dummy" ${TEST_EXECUTABLE}
|
||||
NAME MQTT
|
||||
COMMAND ${JRUN_CTEST_COMMAND} --args "mqtt" ${TEST_EXECUTABLE}
|
||||
)
|
||||
|
||||
set_tests_properties(
|
||||
Led Sensor DNS
|
||||
DUMMY Sensors DNS MQTT
|
||||
PROPERTIES
|
||||
TIMEOUT 50
|
||||
)
|
||||
|
||||
@@ -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 */
|
||||
@@ -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_ */
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "McuLog.h"
|
||||
#include "test_sensors.h"
|
||||
#include "test_dns_resolver.h"
|
||||
#include "test_mqtt_client.h"
|
||||
|
||||
|
||||
static void TestArgFailed(void) {
|
||||
@@ -42,10 +43,19 @@ void Tests_Run(void) {
|
||||
RUN_TEST(TestSensors_Test);
|
||||
} else if (McuUtility_strcmp(buf, "dns")==0) {
|
||||
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 {
|
||||
RUN_TEST(TestArgFailed);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
RUN_TEST(TestArgFailed);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user