No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

005-testing-c.7 1.4KB

TESTING-C(7)       FreeBSD Miscellaneous Information Manual       TESTING-C(7)

     Testing C – a simple unit testing setup

     This is a simple approach to unit testing in C that I've used in a couple
     projects.  At the bottom of a C file with some code I want to test, I

           #ifdef TEST
           #include <assert.h>

           int main(void) {


     This file normally produces a .o to be linked into the main binary.  For
     testing, I produce separate binaries and run them with make(1):

           TESTS = foo.t bar.t

           .SUFFIXES: .t

                   $(CC) $(CFLAGS) -DTEST $(LDFLAGS) $< $(LDLIBS) -o $@

           test: $(TESTS)
                   set -e; $(TESTS:%=./%;)

     Note that the test binaries aren't linked with the rest of the code, so
     there is potential for simple stubbing or mocking.

     To get the best output from C's simple assert(3), it's best to assert the
     result of a helper function which takes the expected output and the test
     input, rather than calling assert(3) inside the helper function.  This
     way, the message printed by the assert failure contains a useful line
     number and the expected output rather than just variable names.

     For a real example, check term.c: from my
     IRC client project.

     C. McEnroe <>

Causal Agency                  December 21, 2018                 Causal Agency