17
\$\begingroup\$

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?

\$\endgroup\$
2

6 Answers 6

4
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Bought a the ePub version, let's see if it is any good :) \$\endgroup\$
    – Johan
    Commented Nov 3, 2011 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
    Commented Nov 7, 2011 at 18:51
  • \$\begingroup\$ Accepted since the book pushed me in the right direction. \$\endgroup\$
    – Johan
    Commented Nov 18, 2011 at 10:08
5
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\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
    Commented Nov 3, 2011 at 10:32
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
2
  • 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
    Commented Mar 16, 2017 at 9:44
  • 2
    \$\begingroup\$ @pipe Yes, but the question (product recommendation essentially) begs for answers like this one. \$\endgroup\$ Commented Mar 16, 2017 at 10:20
0
\$\begingroup\$

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

http://www.eetimes.com/design/embedded/4007177/Doing-C-code-unit-testing-on-a-shoestring-Part-1-The-basics-and-the-tools

I would also agree with everything that Austin has mentioned.

\$\endgroup\$
-2
\$\begingroup\$

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

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Static code analysis can't help execute and test the code, so lint it not really helpful. \$\endgroup\$
    – Johan
    Commented Nov 3, 2011 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
    Commented Nov 3, 2011 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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