Skip to content

Getting Started

This tutorial takes you from an empty directory to a passing (and then a failing) AceUnit test run. It targets AceUnit 3.1.0.

  • AceUnit is installed under /usr/local (see Installation). If you installed elsewhere, replace /usr/local with your PREFIX.
  • You have a C compiler (cc) and objdump on your PATH.
  • Directoryleapyear/
    • leapyear.h the code under test (interface)
    • leapyear.c the code under test (implementation)
    • leapyear_test.c your tests
    • testcases.c generated by aceunit
  1. Create the code under test.

    leapyear.h
    #ifndef LEAPYEAR_H
    #define LEAPYEAR_H
    #include <stdbool.h>
    extern bool isLeapYear(int year);
    #endif
    leapyear.c
    #include "leapyear.h"
    bool isLeapYear(int year) {
    return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }
  2. Write the tests. Any function whose name starts with test is a test case. No registration, no macros.

    leapyear_test.c
    #include "leapyear.h"
    #include <assert.h>
    void testLeapYears(void) {
    assert(isLeapYear(0));
    assert(isLeapYear(4));
    assert(isLeapYear(400));
    }
    void testNonLeapYears(void) {
    assert(!isLeapYear(1));
    assert(!isLeapYear(100));
    }
  3. Compile everything to object files.

    Terminal window
    cc -I /usr/local/include -c leapyear.c leapyear_test.c
  4. Generate the fixture table. aceunit reads the symbol table of the test object file and writes C source to stdout.

    Terminal window
    aceunit leapyear_test.o >testcases.c

    The generated file looks like this:

    testcases.c
    #include <aceunit.h>
    extern void testLeapYears(void);
    extern void testNonLeapYears(void);
    const aceunit_func_t testcases_leapyear_test[] = {
    testLeapYears,
    testNonLeapYears,
    NULL,
    };
    const AceUnit_Fixture_t fixture_leapyear_test = {
    NULL,
    NULL,
    NULL,
    NULL,
    testcases_leapyear_test,
    };
    const AceUnit_Fixture_t *fixtures[] = {
    &fixture_leapyear_test,
    NULL,
    };
  5. Compile the fixture table and link with a runner. This tutorial uses the Fork runner, which keeps going after a failure.

    Terminal window
    cc -I /usr/local/include -c testcases.c
    cc -o leapyear_test leapyear.o leapyear_test.o testcases.o /usr/local/lib/libaceunit-fork.a
  6. Run the tests.

    Terminal window
    ./leapyear_test

    Expected output (on stderr), exit status 0:

    ./leapyear_test: 2 test cases, 2 successful, 0 failed.

Change assert(!isLeapYear(100)); to assert(!isLeapYear(2000));, then recompile, regenerate, and run again:

Terminal window
cc -I /usr/local/include -c leapyear_test.c
cc -o leapyear_test leapyear.o leapyear_test.o testcases.o /usr/local/lib/libaceunit-fork.a
./leapyear_test
leapyear_test: leapyear_test.c:12: testNonLeapYears: Assertion `!isLeapYear(2000)' failed.
./leapyear_test: 2 test cases, 1 successful, 1 failed.

The exact assertion message comes from your C library (this is glibc, which prefixes the program name). The executable exits with status 1 (EXIT_FAILURE), so make and CI notice the failure.

The same workflow as a Makefile, adapted from the AceUnit README:

Makefile
ACEUNIT_HOME=/usr/local
ACEUNIT_LIBRARY=$(ACEUNIT_HOME)/lib/libaceunit-fork.a
CPPFLAGS+=-I $(ACEUNIT_HOME)/include
.PHONY: all
all: test
.PHONY: test
test: leapyear_test
./$^
leapyear_test: leapyear_test.o leapyear.o testcases.o $(ACEUNIT_LIBRARY)
testcases.c: leapyear_test.o
$(ACEUNIT_HOME)/bin/aceunit $^ >$@
.PHONY: clean
clean::
$(RM) *.o leapyear_test testcases.c

Run make to build and test, make clean to start over.