Essential Open‑Source C Unit Testing Frameworks and Unity on STM32
This note introduces several open‑source C unit‑testing frameworks, compares their features, and provides a step‑by‑step guide for porting and using the Unity framework on an STM32 platform, including configuration, test case writing, and result interpretation.
Introduction
The author reflects on the importance of testing in software development, warns job‑seekers about preferring development roles over testing roles, and distinguishes between testing one's own code and testing others' code.
Open‑Source Testing Frameworks
Four lightweight C unit‑testing frameworks are presented:
Unity – a small C framework suitable for embedded testing (latest release v2.5.0). Repository:
https://github.com/ThrowTheSwitch/Unity/releases
CuTest – a minimal framework consisting of only CuTest.c and CuTest.h. Repository:
https://sourceforge.net/projects/cutest/
Embedded Unit – a pure‑standard‑C framework for embedded C, with no dependency on the C standard library and static allocation only. Repository:
https://sourceforge.net/projects/embunit
Google Test (gtest) – a C++‑based framework that also supports C, offering rich assertions, automatic test discovery, death tests, fixtures, and parameterized tests. Repository:
https://github.com/google/googletest/releases/tag/release-1.8.0
Using Unity on STM32
The author describes how to integrate Unity into a Keil project for an STM32 target:
Copy unity.c, unity.h, and unity_internals.h into the project and add unity.c to the build.
Create a platform‑specific unity_config.h that includes hardware headers and defines macros for UART output and initialization.
Enable the inclusion of unity_config.h in unity_internals.h by defining the appropriate macro.
In main.c include unity.h and call UNITY_BEGIN() (which also performs UART initialization), then run each test with RUN_TEST(test_function), and finish with UNITY_END() to obtain the result.
Example Test Case
A leap‑year checking function is tested. The test uses the assertions TEST_ASSERT_TRUE and TEST_ASSERT_FALSE. The required setUp and tearDown functions are omitted because no special initialization is needed.
Running the test yields a passing result; modifying the expected year causes a failure, and the output pinpoints the failing line.
Books and Further Resources
The note mentions two books that cover unit testing and embedded C development, providing additional background on static analysis tools such as pc‑lint and SourceMonitor, as well as CI tools like GitLab.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
