Transitioning from JUnit to C Unit Testing in Embedded Development

Are there any projects that make C unit testing as easy as unit testing in Java with JUnit?

I worked on an embedded system written in C this summer, taking over an existing project. I’m used to writing unit tests in Java using JUnit, but I struggled with how to write unit tests for existing and new C code.

Any help around specific to embedded development, especially cross-compiling to arm-linux, would be greatly appreciated.

I have about five years of experience in C unit testing, particularly in embedded systems. One C unit testing framework that stands out is Check (Check | Unit testing framework for C). Here’s a list of other C unit testing frameworks:

  1. AceUnit: Mimics JUnit 4.x with reflection-like capabilities. Suitable for embedded software with no standard library functions. AceUnit homepage.
  2. GNU Autounit: Similar to Check, uses GLib, and runs tests in a separate address space. GNU Autounit homepage.
  3. cUnit: Uses GLib but does not fork to protect the address space.
  4. CUnit: Standard C, with plans for a Win32 GUI. Does not fork. CUnit homepage.
  5. CuTest: Simple framework with one .c and one .h file. CuTest homepage.
  6. CppUnit: For C++, can test C code. Requires a C++ compiler. CppUnit homepage.
  7. embUnit: For embedded systems, superseded by AceUnit. Embedded Unit homepage.
  8. MinUnit: Minimal set of macros for unit testing. MinUnit homepage.
  9. CUnit for Mr. Ando: New implementation in early development. CUnit for Mr. Ando homepage.
  10. CMocka: Test framework for C with mock objects. CMocka homepage.
  11. Criterion: Cross-platform with support for multiple formats. Each test runs in its own process. Criterion homepage.
  12. HWUT: General unit test tool with support for C, can create Makefiles and generate test cases. HWUT homepage.
  13. CGreen: Modern framework for C and C++, includes BDD notation and a mocking library. CGreen homepage.

I’ve been working with C unit testing frameworks for about six years now. Recently, I started using the CuTest framework for C unit testing, and it’s been a game-changer for me, especially in embedded systems. You can check it out here: CuTest homepage.

It’s ideal for embedded systems due to its lightweight and simple nature. I had no issues running it on both the target platform and the desktop. Here’s what you need to do:

  1. Include a header file wherever you call CuTest routines.
  2. Compile/link a single additional C file into the image.
  3. Add some simple code to main to set up and call the unit tests. This can be in a special main() function compiled if UNITTEST is defined during the build.

The system needs to support a heap and some stdio functionality, but the code is simple enough to work around these requirements if necessary. With extern "C" {} blocks, it also supports testing C++ code.

I’ve been in the field of C unit testing for over seven years, and I have to agree with Sam. CuTest is fantastic for developing on platforms like the Nintendo DS and QNX systems. It’s really easy to set up and use. Here’s the CuTest homepage again: CuTest homepage.

The included make-tests.sh script means you don’t have to manually write your main() or add in all your tests. Just add your function to test, and you’re good to go! I’ve been using it for embedded systems, and it’s already catching legacy weaknesses.

Plus, when paired with fff.h, it’s great for mocking functions and more. It’s a solid choice for anyone looking to simplify their C unit testing process.