Site icon Cove Mountain Software

Introducing CppUTest For FreeRTOS

CppUTest for FreeRTOS (cpputest-for-freertos) is a host PC based unit testing library supporting the unit testing of well-formed FreeRTOS based code. The purpose is to enable rapid TDD unit testing with host-PC based testing of FreeRTOS based firmware. It provides various FreeRTOS functions as non-functional linking only replacements or ‘fakes’ providing equivalent functionality for the unit-under-test. FreeRTOS timers are a great example, where this library provides functional-but-fake timers, where control of the fake timers is left to the unit tests themselves.

Alert readers might already be asking themselves: what are “well-formed” modules? Well-formed in this context means a module with a single thread employing a single blocking mechanism, such as a queue or semaphore. When unblocked the primary behavior of the thread is activated. A well-known example would be an active object. Frankly, any thread design employed in firmware should be well-formed. Like many others, I consider the mixing of RTOS blocking and locking mechanism’s to be a recipe for a bad-cake, if not disaster. Two separate examples are provided to demonstrate how to design the module to enable unit testing in this environment. Other approaches are possible, but the more a unit-under-test deviates from the above pattern, the more difficult it will be to unit test.

So what is the benefit? Easier TDD. Faster TDD. Host-PC unit testing. Module privates are easier to keep private. How?

Take a look at timers. If a module is using FreeRTOS timers to create timed behavior and we want to employ host-PC based unit testing, how do we do that? Options include:

Where does the library stand currently? See the below table (reflecting v1.1.0, as of Nov 14, 2024). Please note that none of the provided “fake but functional” items will ever block. Blocking is bad while unit testing, as I want my tests to execute fast and reliably.

ItemStatus
FreeRTOS versionCurrently developed against v11.1.0
Task APILink only. Tasks are never created.
QueuesFake but functional.
Queue SetsFake but functional.
TimersFake but functional, with namespace’d control methods provided to allow unit tests to control the passage of time. Additionally, timers are coordinated with vTaskDelay, xTaskGetTickCount, and related APIs.
SemaphoresFake but functional.
MutexesFake but functional. Additionally (kinda cool), after the conclusion of each unit test, the library will confirm that all mutexes are destroyed OR are in an unlocked state.
Direct to task notificationsTODO
Stream BuffersTODO
Message BuffersTODO
Event GroupsTODO

An early inspiration for this effort may be found in this past post: “Unit Testing of Active Objects…” Take a look to learn more.

Licensing is GPL or Commercial. Commercial product-line licenses will be free for any entity that engages in a consulting or code review engagement with myself. Please contact me for details.

I hope this library will grow and prove useful to many firmware teams using FreeRTOS. Take a look at it on github!

Related references:

Exit mobile version