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.
Before you begin
Section titled “Before you begin”- AceUnit is installed under
/usr/local(see Installation). If you installed elsewhere, replace/usr/localwith yourPREFIX. - You have a C compiler (
cc) andobjdumpon yourPATH.
What you will build
Section titled “What you will build”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
Write and run a test
Section titled “Write and run a test”-
Create the code under test.
leapyear.h #ifndef LEAPYEAR_H#define LEAPYEAR_H#include <stdbool.h>extern bool isLeapYear(int year);#endifleapyear.c #include "leapyear.h"bool isLeapYear(int year) {return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;} -
Write the tests. Any function whose name starts with
testis 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));} -
Compile everything to object files.
Terminal window cc -I /usr/local/include -c leapyear.c leapyear_test.c -
Generate the fixture table.
aceunitreads the symbol table of the test object file and writes C source to stdout.Terminal window aceunit leapyear_test.o >testcases.cThe 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,}; -
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.ccc -o leapyear_test leapyear.o leapyear_test.o testcases.o /usr/local/lib/libaceunit-fork.a -
Run the tests.
Terminal window ./leapyear_testExpected output (on stderr), exit status
0:./leapyear_test: 2 test cases, 2 successful, 0 failed.
See a test fail
Section titled “See a test fail”Change assert(!isLeapYear(100)); to assert(!isLeapYear(2000));, then recompile, regenerate, and run again:
cc -I /usr/local/include -c leapyear_test.ccc -o leapyear_test leapyear.o leapyear_test.o testcases.o /usr/local/lib/libaceunit-fork.a./leapyear_testleapyear_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.
Automate it with make
Section titled “Automate it with make”The same workflow as a Makefile, adapted from the AceUnit README:
ACEUNIT_HOME=/usr/localACEUNIT_LIBRARY=$(ACEUNIT_HOME)/lib/libaceunit-fork.aCPPFLAGS+=-I $(ACEUNIT_HOME)/include
.PHONY: allall: test
.PHONY: testtest: leapyear_test ./$^
leapyear_test: leapyear_test.o leapyear.o testcases.o $(ACEUNIT_LIBRARY)
testcases.c: leapyear_test.o $(ACEUNIT_HOME)/bin/aceunit $^ >$@
.PHONY: cleanclean:: $(RM) *.o leapyear_test testcases.cRun make to build and test, make clean to start over.