I am facing a lot of issues with Unity Test Framework.
pio test
Verbosity level can be increased via `-v, -vv, or -vvv` option
Collected 1 tests
Processing * in esp32dev environment
----------------------------------------------------------------------------------------------------------------------------------Building & Uploading...
Library Manager: Installing throwtheswitch/Unity @ ^2.6.0
-------------------------------------------- esp32dev:* [ERRORED] Took 65.06 seconds --------------------------------------------
Processing * in native environment
----------------------------------------------------------------------------------------------------------------------------------Building...
.pio\build\native\unity_config_build\unity_config.o: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\native\program.exe] Error 1
Building stage has failed, see errors above. Use `pio test -vvv` option to enable verbose output.
---------------------------------------------- native:* [ERRORED] Took 4.90 seconds ----------------------------------------------
============================================================ SUMMARY ============================================================
Environment Test Status Duration
------------- ------ -------- ------------
esp32dev * ERRORED 00:01:05.060
native * ERRORED 00:00:04.903
=========================================== 2 test cases: 0 succeeded in 00:01:09.963 ===========================================
PS C:\Users\KMani.Balan\Documents\PlatformIO\Projects\UnitTesting2>
Simple test.cpp file
#include <unity.h>
// Function to be tested
int add(int a, int b) {
return a + b;
}
// Test case for addition
void test_add_function() {
TEST_ASSERT_EQUAL(5, add(2, 3));
TEST_ASSERT_EQUAL(0, add(-1, 1));
TEST_ASSERT_EQUAL(-3, add(-1, -2));
}
void setup() {
// Initialize Unity Test Framework
UNITY_BEGIN();
// Run test cases
RUN_TEST(test_add_function);
// End Unity Test Framework
UNITY_END();
}
void loop() {
// Not used in testing; left empty
}
platform.ino
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
test_ignore = test_desktop
monitor_speed = 115200
lib_deps = throwtheswitch/Unity@2.5.2
[env:native]
platform = native
test_ignore = test_embedded
lib_deps = throwtheswitch/Unity@2.5.2
So I was going through how to implement unity with platformio and came across the same issue as yours. I managed to solve it so for any future person you can see my implementation:
Unit Testing
So the issue is that when testing in [native]
env you have to have int main
as entry point and not setup() loop()
way of doing it.
Code
#include <unity.h>
#include "add.h"
void test_addition() {
TEST_ASSERT_EQUAL(5, add(2, 3));
TEST_ASSERT_EQUAL(-1, add(-2, 1));
TEST_ASSERT_EQUAL(0, add(0, 0));
}
void setUp() {}
void tearDown() {}
int main() {
UNITY_BEGIN(); // Initialize Unity
RUN_TEST(test_addition); // Run the test
UNITY_END(); // End Unity
return 0;
}
For arduino Testing Code
#include <Arduino.h>
#include <unity.h>
#include "WiFiManager.h"
WiFiManager wifiManager;
// Mock Wi-Fi credentials
const char* TEST_SSID = "TestSSID";
const char* TEST_PASSWORD = "TestPassword";
// Test: Wi-Fi connection timeout
void test_wifi_connection_timeout() {
wifiManager.begin(TEST_SSID, TEST_PASSWORD);
TEST_ASSERT_FALSE(wifiManager.isConnected());
}
// Test: Get IP address when disconnected
void test_get_ip_address_disconnected() {
String ip = wifiManager.getIPAddress();
TEST_ASSERT_EQUAL_STRING("Not Connected", ip.c_str());
}
void setup() {
UNITY_BEGIN();
RUN_TEST(test_wifi_connection_timeout);
RUN_TEST(test_get_ip_address_disconnected);
UNITY_END();
}
void loop() {
// Not used for unit testing
}
The ini
file looks like this:
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:ESP32_S3_DEV_4MB_QD_No_PSRAM]
platform = espressif32
board = ESP32_S3_DEV_4MB_QD_No_PSRAM
framework = arduino
test_build_src = true ; Include the main project source in test builds
monitor_speed = 115200
test_ignore = test_desktop
[env:native]
platform = native
test_ignore = test_device
test_build_src = false