22 June, 2009

Unit test framework for legacy C application code

Adding a bunch of function to a big application (a 4GL) that was developed long ago and over the course of 10 years (1987 - 1997) is a risky business. Particularly when the only extant testing code is written in the 4GL – equivalent to only being able to test an HTML browser by getting it to process HTML.

I've certainly learned a lot since I took on my first product development job. One of the practices I'm still learning is good unit testing. I've learned to think in terms of the JUnit unit testing framework. Such a framework is important to focus the testing code on testing instead of housekeeping and to define standard reports. There are several unit testing frameworks for C listed on OpenSourceTesting.org.

The following list contains the subset of the test frameworks that I've considered for my project. As noted below, I've picked up the Cgreen framework with good results.

Boost.Test
This framework is in active use, but it's primarily used for C++ applications. The API looks simple enough that it could be reasonably used to test C functions.
C Unit Test System
Defunct project – there's a note on the sourceforge project forum from 2003 asking if there are any users.
Cgreen
Unit testing strictly for C with a facility for mocking functions.
C Unit
Abandoned project. No response to inquiries or code updates since 2006.
CuTest
A very simple implementation of unit test framework. Some activity on the project. No documentation found.
RCUNIT
Abandoned project. No activity since 0.9 in 2006
Simple C Test
Abandoned project. No activity since 2005. Some documentation is provided.
Test soon
Abandoned project. No activity since 2007. Seems to focus on flexibility to structure tests and not so much on enabling easy authoring.
TUT test framework
Interesting framework in C++ compiled into each test suite. Not clear how failures should be detected, not designed to automatically recover from faults (SEGV) so further test cases can run.
unit--
Abandoned project. No activity since 2006.
Unity & CMock
Nice looking unit test and mock tools for C targeting embedded systems developers. Cmock requires Ruby and Rake.

The other tools listed were apparently only for C++ or objective C.

After some examination and experimentation, the Cgreen package seems to be the best fit. The mock function capability is a useful stand-in for the mock objects one would use for an object-oriented environment.

Updating the logging code with log4c

Having been delighted at my introduction to log4j in 2001, I was anxious to find a flexible logging and tracing framework suitable for introduction in the C program I'm updating. All the Google searches I tried produced results that pointed at Java, Python or PHP tools. I'd begun to despair and was thinking about putting something together on my own. It was Wikipedia's Log4J that finally pointed me to the Log4c logging and tracing framework.

I'm still working on getting it fully integrated, but the Log4c download was easy to compile on Sun's Solaris 10 (binaries for Linux are provided). One thing to note is that the CVS repository only has sources for automake, so the installation instructions are a couple steps removed.

It's not so easy to find documentation, though some effort seems to have gone into preparing some. The links for documentation go to a SourceForge site that yields an entry saying 1.0.0 with no content. I was eventually able to find a good guide to the use of Log4C on slideshare.

I haven't tried all the options, yet, but Log4C provides an elegant API that looks like it will be quite efficient. It will allow logging and tracing calls to be added with little impact on the application. It also promises to support generating syslog entries and rolling log files.

 

Copyright 2009-2010 John Bito. Creative Commons License
This work is licensed under a Creative Commons Attribution-NoDerivs 3.0 Unported License.