3.2. Setting up the money tests

Since we are creating a library to handle money, we will first create a header money.h, and a file to contain our unit tests, check_money.c (there is a pun there, but no matter...). To manage everything we'll use Autoconf/Automake for this example. (One could do something similar with ordinary makefiles, but in the author's opinion, it is generally easier to use Autoconf/Automake than bare makefiles, and it provides built-in support for running tests). Here is the Makefile.am:

TESTS=check_money
noinst_PROGRAMS=check_money
check_money_SOURCES=  money.h money.c check_money.c
check_money_INCLUDES= @CHECK_CFLAGS@
check_money_LDADD= @CHECK_LIBS@

Special mumbo jumbo to use in configure.in is:

AC_INIT()
AM_INIT_AUTOMAKE()
AC_PROG_CC
AM_PATH_CHECK()
AC_OUTPUT(Makefile)

This will ensure that things link up properly with Check by defining the appropriate compiler and linker flags as CHECK_CFLAGS and CHECK_LIBS. It also makes sure that we can only compile in an environment that has Check. The money.h header should only contain the standard #ifndef MONEY_H stuff, money.c should be empty, and check_money.c should only contain an empty main function. Run this with make -k check, after going through the setups to get autoconf and friends working. If all goes well, make should report that our tests passed. No surprise, because there aren't any tests to fail.

The AM_PATH_CHECK() macro is defined in the file check.m4 which is installed by Check. If you see warnings from automake or aclocal this most certainly means that you forgot to call aclocal or that aclocal can't find check.m4. AM_PATH_CHECK() has some optional parameters that you might find useful:

AM_PATH_CHECK([MINIMUM-VERSION,[ACTION-IF-FOUND[,ACTION-IF-NOT-FOUND]]])

One way to run the autoconf/automake tools is shown below in a simple shell script. The variable CHECK_DIR should point to the directory where check.m4 is installed. If you install it in a standard location, e.g. /usr/share/aclocal, then you won't need the variable defined.

#!/bin/sh
if [ -n "$CHECK_DIR" ]; then
    aclocal -I $CHECK_DIR
else
    aclocal
fi
autoconf
autoheader
automake --add-missing

This shell script and the above configure.in were run on a Red Hat 9 box with Autoconf version 2.13 and automake (GNU automake) 1.4-p6. As the saying goes, "Your mileage may vary..."