views:

6871

answers:

23
+49  Q: 

Unit Testing C Code

I worked on an embedded system this summer written in straight C. It was an existing project that the company I work for had taken over. I have become quite accustomed to writing unit tests in Java using JUnit but was at a loss as to the best way to write unit tests for existing code (which needed refactoring) as well as new code added to the system.

Are there any projects out there that make unit testing plain C code as easy as unit testing Java code with JUnit? Any insight that would apply specifically to embedded development (cross-compiling to arm-linux platform) would be greatly appreciated.

+28  A: 

One unit testing framework in C is Check; a list of unit testing frameworks in C can be found here. Depending on how many standard library functions your runtime has, you may or not be able to use one of those.

Adam Rosenfield
Initially, Check looks very solid. I will have to see how it holds up under the fire of real use... but it definitely looks like it may fit the bill.
Paul Osborne
We use check for unit testing code on our embedded systems. For the most part check was a good choice but now we are working on systems running on uClinux and since check requires fork it doesn't work on those systems. :/
David Holm
the "here" link is broken and I think that this is the new link: http://check.sourceforge.net/doc/check.html/check_3.html#SEC3
lillq
+1 for Check. I have used it in several projects in which the largest parts of actual functionality was implemented as a library.
hillu
+2  A: 

Google has excellent testing framework. http://code.google.com/p/googletest/wiki/GoogleTestPrimer

And yes, as far as I see it will work with plain C, i.e. doesn't require C++ features (may require C++ compiler, not sure).

phjr
Will google's framework work with pure C? A quick glance at the page suggests it's a C++ framework.
Dana
Google Test is excellent, but it's very much a C++ framework. It is pretty portable and can be used to test C if you had to.
Josh Kelley
+2  A: 

cmockery at http://code.google.com/p/cmockery/

landon9720
+2  A: 

If you are familiar with JUnit then I recommend CppUnit. http://cppunit.sourceforge.net/cppunit-wiki

That is assuming you have c++ compiler to do the unit tests. if not then I have to agree with Adam Rosenfield that check is what you want.

Kwondri
The question is about C, not C++
1800 INFORMATION
No, but C++ can interface to C libraries. So it may in fact be perfectly fine to test C libraries using a C++ unit test framework. (My company does that very thing by the way and it's so much easier than using C unit test frameworks.)
Kevin
I do the same thing. We have a utilities library written in C that we use underneath our C++ code and scripting languages. We use CppUnit for the tests and it's working out pretty well since we can use the same framework for both C and C++.
Jyaan
A: 

One technique to use is to develop the unit test code with a C++ xUnit framework (and C++ compiler), while maintaining the source for the target system as C modules.

Make sure you regularly compile your C source under your cross-compiler, automatically with your unit tests if possible.

quamrana
+4  A: 

There is CUnit

And Embedded Unit is unit testing framework for Embedded C System. Its design was copied from JUnit and CUnit and more, and then adapted somewhat for Embedded C System. Embedded Unit does not require std C libs. All objects are allocated to const area.

And Tessy automates the unit testing of embedded software.

Prakash
I tried `embunit` and was disappointed by it.
Craig McQueen
E.g. see a bug report I submitted, as well as another bug report that is unactioned for 3 years.
Craig McQueen
+12  A: 

Minunit is an incredibly simple unit testing framework. I'm using it to unit test c microcontroller code for avr.

Matteo Caprari
It's minunit, see http://www.jera.com/techinfo/jtns/jtn002.html
philippe
A: 

First, look here: http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C

My company has a C library our customers use. We use CxxTest (a C++ unit test library) to test the code. CppUnit will also work. If you're stuck in C, I'd recommend RCUNIT (but CUnit is good too).

Kevin
+3  A: 

I use CxxTest for an embedded c/c++ environment (primarily C++).

I prefer CxxTest because it has a perl/python script to build the test runner. After a small slope to get it setup (smaller still since you don't have to write the test runner), it's pretty easy to use (includes samples and useful documentation). The most work was setting up the 'hardware' the code accesses so I could unit/module test effectively. After that it's easy to add new unit test cases.

As mentioned previously it is a C/C++ unit test framework. So you will need a C++ compiler.

CxxTest User Guide CxxTest Wiki

Zing-
The compiler you need may be c++ but the code you are testing can still be C. CxxTest is a very easy framework to use
David Sykes
+8  A: 

You also might want to take a look at libtap, a C testing framework which outputs the Test Anything Protocol (TAP) and thus integrates well with a variety of tools coming out for this technology. It's mostly used in the dynamic language world, but it's easy to use and becoming very popular.

Ovid
I hand-rolled my own libtap-equivalent for my own projects, but now that I know this exists, I won't have to maintain mine anymore. Cool!
ephemient
`ok(TESTING==IsSimple(), "libtap is super easy to use")`
AShelly
+4  A: 

I don't use a framework, I just use autotools "check" target support. Implement a "main" and use assert(s).

My test dir Makefile.am(s) look like:

check_PROGRAMS = test_oe_amqp

test_oe_amqp_SOURCES = test_oe_amqp.c
test_oe_amqp_LDADD = -L$(top_builddir)/components/common -loecommon
test_oe_amqp_CFLAGS = -I$(top_srcdir)/components/common -static

TESTS = test_oe_amqp
navicore
We are not using autotools (though it would be nice to move over at some point). Historically, I have used the main method for testing purposes and it is not a bad solution.
Paul Osborne
+2  A: 

Cmockery is a recently launched Google Code project that consists on a very simple to use C library for writing unit tests.

Alejandro Bologna
+10  A: 

I'm currently using the CuTest unit test framework:

http://cutest.sourceforge.net/

It's ideal for embedded systems as it's very lightweight and simple. I had no problems getting it to work on the target platform as well as on the desktop. In addition to writing the unit tests, all that's required is:

  • a header file included wherever you're calling the CuTest routines
  • a single additional 'C' file to be compiled/linked into the image
  • some simple code added to to main to set up and call the unit tests - I just have this in a special main() function that gets compiled if UNITTEST is defined during the build.

The system needs to support a heap and some stdio functionality (which not all embedded systems have). But the code is simple enough that you could probably work in alternatives to those requirements if your platform doesn't have them.

With some judicious use of extern "C"{} blocks it also supports testing C++ just fine.

Michael Burr
I'll second the vote for CuTest. I've been using it for developing homebrew on the Nintendo DS and haven't had any difficulty getting it set up or using it.
Theran
A: 

I voted for CXXTest but can't edit the response. So'll I'll just add it here.

One of the great features of CXXTest is it's support for Mocks. As it turns out the Mocks in CXXTest are really only meant for regular functions so they work really well for C.

One problem is that you need to use namespaces when invoking the Mocks. This may or may not be a problem for you.

Tom
A: 

In case you are targeting Win32 platforms or NT kernel mode, you should have a look at cfix.

Johannes Passing
+1  A: 

I used RCUNIT to do some unit testing for embedded code on PC before testing on the target. Good hardware interface abstraction is important else endianness and memory mapped registers are going to kill you.

Gerhard
A: 

If you're still on the hunt for test frameworks, CUnitWin32 is one for the Win32/NT platform.

This solves one fundamental problem that I faced with other testing frameworks. Namely global/static variables are in a deterministic state because each test is executed as a separate process.

Dushara
+8  A: 

Personally I like the Google Test framework.

The real difficulty in testing C code is breaking the dependencies on external modules so you can isolate code in units. This can be especially problematic when you are trying to get tests around legacy code. In this case I often find myself using the linker to use stubs functions in tests.

This is what people are referring to when they talk about "seams". In C your only option really is to use the pre-processor or the linker to mock out your dependencies.

A typical test suite in one of my C projects might look like this:

#include "myimplementationfile.c"
#include <gtest/gtest.h>

// Mock out external dependency on mylogger.o
void Logger_log(...){}

TEST(FactorialTest, Zero) {
    EXPECT_EQ(1, Factorial(0));
}

Note that you are actually including the C file and not the header file. This gives the advantage of access to all the static data members. Here I mock out my logger (which might be in logger.o and give an empty implementation. This means that the test file compiles and links independently from the rest of the code base and executes in isolation.

As for cross-compiling the code, for this to work you need good facilities on the target. I have done this with googletest cross compiled to Linux on a PowerPC architecture. This makes sense because there you have a full shell and os to gather your results. For less rich environments (which I classify as anything without a full OS) you should just build and run on the host. You should do this anyway so you can run the tests automatically as part of the build.

I find testing C++ code is generally much easier due to the fact that OO code is in general much less coupled than procedural (of course this depends a lot on coding style). Also in C++ you can use tricks like dependency injection and method overriding to get seams into code that is otherwise encapsulated.

Michael Feathers has an excellent book about testing legacy code. In one chapter he covers techniques for dealing with non-OO code which I highly recommend.

Edit: I've written a blog post about unit testing procedural code, with source available on GitHub.

mikelong
+3  A: 

Michael Feather's book "Working Effectively with Legacy Code" presents a lot of techniques specific to unit testing C development. Their are techniques related to dependency injection that are specific to C which I haven't seen anywhere else.

Frank Schwieterman
A: 

LibU (http://koanlogic.com/libu) has an unit test module that allows explicit test suite/case dependencies, test isolation, parallel execution and a customizable report formatter (default formats are xml and txt).

The library is BSD licensed and contains many other useful modules - networking, debugging, commonly used data structures, configuration, etc. - should you need them in your projects ...

bongo
A: 

other than my obvious bias

http://code.google.com/p/seatest/

is a nice simple way to unit test C code. mimics xUnit

Keith Nicholas
+2  A: 

I didn't get far testing a legacy C application before I started looking for a way to mock functions. I needed mocks badly to isolate the C file I want to test from others. I gave cmock a try and I think I will adopt it.

Cmock scans header files and generates mock functions based on prototypes it finds. Mocks will allow you to test a C file in perfect isolation. All you will have to do is to link your test file with mocks instead of your real object files.

Another advantage of cmock is that it will validate parameters passed to mocked functions, and it will let you specify what return value the mocks should provide. This is very useful to test different flows of execution in your functions.

Tests consist of the typical testA(), testB() functions in which you build expectations, call functions to test and check asserts.

The last step is to generate a runner for your tests with unity. Cmock is tied to the unity test framework. Unity is as easy to learn as any other unit test framework.

Well worth a try and quite easy to grasp:

http://sourceforge.net/apps/trac/cmock/wiki

Update 1

Another framework I am investigating is Cmockery.

http://code.google.com/p/cmockery/

It is a pure C framework supporting unit testing and mocking. It has no dependency on ruby (contrary to Cmock) and it has very little dependency on external libs.

It requires a bit more manual work to setup mocks because it does no code generation. That does not represent a lot of work for an existing project since prototypes won't change much: once you have your mocks, you won't need to change them for a while (this is my case). Extra typing provides complete control of mocks. If there is something you don't like, you simply change your mock.

No need of a special test runner. You only need need to create an array of tests and pass it to a run_tests function. A bit more manual work here too but I definitely like the idea of a self-contained autonomous framework.

Plus it contains some nifty C tricks I didn't know.

Overall Cmockery needs a bit more understanding of mocks to get started. Examples should help you overcome this. It looks like it can do the job with simpler mechanics.

Philippe A.
A: 

try lcut! - http://code.google.com/p/lcut

Tony Bai
Some documentation would be helpful. Project background and goals, a features list, advantages over existing alternatives, etc would be helpful for people who are checking it out for the first time.
Craig McQueen