I'm thinking about how I can use unit tests in my mcu project, and what frameworks I can use to simplify it.

Today I am using a stm32 with OpenOCD-jtag from a Linux pc, where it is all controlled from a classic Makefile and crosscompiled with gcc.

I could create something myself, but if there is a framework that I can use it would be nice. (It is a bonus if the framework can output the result in a format that Jenkins/Hudson can read).

Is there a way to use a unit test framework with an stm32?

  • 3
    \$\begingroup\$ I don't have time to write a full answer, but I've used a lot of the tools and techniques found in these papers and this blog series. In a word: CMock! \$\endgroup\$ – Kevin Vermeer Nov 3 '11 at 12:50

Check out CppUTest, and James Grenning's excellent http://pragprog.com/book/jgade/test-driven-development-for-embedded-c

CppUTest has support for C and C++, and it's got a nice set of Makefile templates that got me started pretty quickly.

  • \$\begingroup\$ Bought a the ePub version, let's see if it is any good :) \$\endgroup\$ – Johan Nov 3 '11 at 15:40
  • \$\begingroup\$ The book is good, but I think that unity (the other framework in that book) will suite my need better. \$\endgroup\$ – Johan Nov 7 '11 at 18:51
  • \$\begingroup\$ Accepted since the book pushed me in the right direction. \$\endgroup\$ – Johan Nov 18 '11 at 10:08

There are a lot of variables which will determine the best unit testing framework to use in you situation. Some items which may affect your choice will be:

  • The target language.
  • What library support is available. eg libc or a cut down version thereof.
  • The operating system of the target. eg None, FreeRTOS, custom.

Most of the xUnit type frameworks will provide some base level of functionality which may be useful. I've used Cunit with some success in the past. (libcunit1-dev package on Ubuntu/Debian). Most frameworks will require libc to be available, some will require additional OS support.

Another altermative which is just 3 lines long is Minunit.

I've found unit testing using the microcontroller as the target to be quite cumbersome as you need to be able to present an environment suitable for downloading tests, running them and then getting results back. Just getting the platform in place which will enable you to do this is a big task.

Another approach that I've taken which has worked for me is to do unit testing on the host, implementing an abstraction layer between the drivers and application code. Since you're using gcc for the target, the code should also compile on the host.

Testing on the compile host is generally so much easier as you have the complete support of the host OS and all its tools. For example, when testing on the host, I have a mocked version of my wireless driver with the same interface as the real driver which runs on the target. The host version uses UDP packets to simulate wireless packet transfer, with the mock driver supporting the ability to drop packets so I can test my protocols.

In the product I was working on, a threaded OS was being used, so the abstraction layer for testing on the host OS used pthreads instead.

While not perfect, the easier it is for you to write and run tests, the more likely you are to implement more test cases. Another benefit of have the code run on different platforms is to test that the code is portable. You'll quickly pick up endian mistakes if the target and host architectures differ.

I'm now a bit off topic, but feel these ideas may help with your choice of test framework and test methods.

  • \$\begingroup\$ I have solved how I get code on the target and I can use gdb in script mode to stop at different breakpoint like test_ok or test_fail (fun-tech.se/stm32/TestSuite/index.php). So I am kind of half way. This is more a question how to build the different "tests". My ideas today is a little bit to unflexible, that is why I started to search for some kind of framework. \$\endgroup\$ – Johan Nov 3 '11 at 10:32

Check out embUnit http://embunit.sourceforge.net/embunit/index.html. It is a embedded C unit test framework with a low footprint.

We successfully used it in a couple of embedded microcontroller projects. Don't expect the options and features you get with a desktop unit test framework. But it's definitely powerfull enough.

It has allot of asserts defined for you, so you don't have to lose allot of time writing custom asserts like with minUnit.


Some time ago I wrote a thorough tutorial on the topic: Unit-testing (embedded) C applications with Ceedling; I use these techniques in a bunch of projects, and I'm quite happy so far.

  • 2
    \$\begingroup\$ This is a link-only answer, and as such will become worthless if the URL change or the link goes down. You should explain the relevant information in the answer, then you can add the link as a reference. \$\endgroup\$ – pipe Mar 16 '17 at 9:44
  • 2
    \$\begingroup\$ @pipe Yes, but the question (product recommendation essentially) begs for answers like this one. \$\endgroup\$ – Dmitry Grigoryev Mar 16 '17 at 10:20

I was just looking into this myself. I found a three part article that outlines a unit test framework here:


I would also agree with everything that Austin has mentioned.


Try lint, but I don't think its for unit testing, its for code analysis.

  • 2
    \$\begingroup\$ Static code analysis can't help execute and test the code, so lint it not really helpful. \$\endgroup\$ – Johan Nov 3 '11 at 10:36
  • 1
    \$\begingroup\$ Perhaps not helpful in the context of unit testing, but everyone should be using some kind of static analysis tool. \$\endgroup\$ – Tim Nov 3 '11 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.