4
In DHCP, a unit test exercises a particular piece of code in
5
isolation. There is a separate unit test per module or API. Each unit
6
test lives in a directory beneath the code it is designed to exercise.
15
Ideally each function would be invoked with every possible type of
16
input, and each branch of every function would be checked. In practice
17
we try to be a bit more pragmatic, and target the most basic
18
operations, as well tricky code, and areas we have seen bugs in the
25
In order to run the unit tests for DHCP, use:
29
This will run all of the unit tests.
31
You can run a single test by going to the appropriate test directory
32
and invoking the test directly:
38
There are also a number of options that you can use when running a
39
test. To see these, use the "-u" flag on the program.
42
Adding a New Unit Test
43
----------------------
45
To add an additional test to an existing test program, you must create
46
a function for the new test in the C source file:
50
static const char *test_desc = "describe the test";
52
t_assert("mynewtest", 1, T_REQUIRED, test_desc);
54
/* ... test code ... */
59
Then add this function to the T_testlist[] array in the file:
61
testspec_t T_testlist[] = {
63
{ mynewtest, "some new test" },
67
Then you should be able to compile and run your new test.
70
Adding a New Unit Test Program
71
------------------------------
73
To add a new program, such as when a new module is added, you can copy
74
the "unit_test_sample.c" file (in this directory) to a new name, add
75
the new file as a target in Makefile.am, and begin adding tests. Do
76
not forget to add it to CVS via "cvs add".
78
If there is no "tests" directory for a given subdirectory, then one
79
must be created. This can be done by:
81
1. Creating the directory:
86
2. Adding the subdirectory to the build system:
88
Add to $subdir/Makefile.am:
92
Add to the AC_OUTPUT macro in configure.ac:
94
$subdir/tests/Makefile
96
3. Create a Makefile.am in the new directory, something like this:
100
check_PROGRAMS = test_foo
104
test_foo_SOURCES = test_foo.c
105
test_foo_LDADD = ../../tests/libt_api.a # plus others...
108
See existing Makefile.am for examples, and the Automake documentation:
110
http://www.gnu.org/software/automake/manual/html_node/Tests.html
116
Here are a few of the most useful functions defined in t_api that you
120
t_assert(const char *component, int anum, int class,
121
const char *what, ...);
123
The name of this function is slightly misleading. It
124
actually just prints out an error message in the test
128
t_info(const char *format, ...);
130
Prints out a message in the test output. You should
131
include "\n" at the end.
134
t_result(int result);
136
Prints out the result in the test output. You should
137
use one of the constants for this:
149
Other static or runtime testing is always an option. For instance, you
150
can use valgrind to check for memory leaks.
153
$Id: HOWTO-unit-test,v 1.2 2007/11/16 11:04:12 shane Exp $