Using C unit testing framework Check without Autotools?

You certainly don't need to learn autotools to use Check in small projects. Let's say our main() is in main.c and our implementation.c have a function that sums 2 ints. (implementation.h contains just the function prototype)

#include "implementation.h"

int sum(int a, int b) {

    return a + b;
}

You can write a test like so:

#include "implementation.h"

#test sum2test
    fail_unless(sum(3, 2) == 5, "sum function borked");
    fail_unless(sum(-3, 2) == -1, "sum function borked");
    fail_unless(sum(3, -2) == 1, "sum function borked");
    fail_unless(sum(-3, -2) == -5, "sum function borked");

Save the file in implementation-test.check (you can pick any name / extension you want, but stay with those if you want to follow my guide) and then run the included awk script that comes with Check. You don't even have to bother with the boilerplate code for the check framework! (for more details man checkmk)

checkmk implementation-test.check >implementation-test.c

The output will be the following:

/*
 * DO NOT EDIT THIS FILE. Generated by checkmk.
 * Edit the original source file "implementation-test.check" instead.
 */

#include <check.h>

#line 1 "implementation-test.check"
#include "implementation.h"

START_TEST(sum2test)
{
#line 4
    fail_unless(sum(3, 2) == 5, "sum function borked");
    fail_unless(sum(-3, 2) == -1, "sum function borked");
    fail_unless(sum(3, -2) == 1, "sum function borked");
    fail_unless(sum(-3, -2) == -5, "sum function borked");
}
END_TEST

int main(void)
{
    Suite *s1 = suite_create("Core");
    TCase *tc1_1 = tcase_create("Core");
    SRunner *sr = srunner_create(s1);
    int nf;

    suite_add_tcase(s1, tc1_1);
    tcase_add_test(tc1_1, sum2test);

    srunner_run_all(sr, CK_ENV);
    nf = srunner_ntests_failed(sr);
    srunner_free(sr);

    return nf == 0 ? 0 : 1;
}

Then just include -lcheck when you compile to get the check library linked in and run the program!

gcc -Wall -o sum2ints-test implementation.c implementation-test.c -lcheck
./sum2ints

Below is a simple makefile to get you started. Save it in sum2ints.makefile and then to build the implementation.c along with main, run:

make -f sum2ints.makefile

To build & run the implementation.c with our implementation-test.c that got created from checkmk, run:

make -f sum2ints.makefile test

-

CFLAGS=-Wall
LIBS=-lcheck

all: sum2ints

sum2ints: main.o implementation.o
gcc -o sum2ints main.o implementation.o

main.o: main.c implementation.h
gcc $(CFLAGS) -c main.c

implementation.o: implementation.c implementation.h
gcc $(CFLAGS) -c implementation.c

test: sum2ints-test
./sum2ints-test

sum2ints-test: implementation-test.o implementation.o
gcc -o sum2ints-test implementation.o implementation-test.o $(LIBS)

implementation-test.o: implementation-test.c implementation.h
gcc $(CFLAGS) -c implementation-test.c

I've prepared a .zip file for you containing all the above.

https://dl.dropbox.com/u/1987095/test-check.zip


The answer of @freestyler is good, but it still uses checkmk, which is not necessary.

This is a minimal example without using checkmk.

Put the following in a file named test.c:

#include <check.h>

START_TEST (sanity_check)
{
    fail_unless(5 == 5, "this should succeed");
    fail_unless(6 == 5, "this should fail");
}
END_TEST

int main(void)
{
    Suite *s1 = suite_create("Core");
    TCase *tc1_1 = tcase_create("Core");
    SRunner *sr = srunner_create(s1);
    int nf;

    suite_add_tcase(s1, tc1_1);
    tcase_add_test(tc1_1, sanity_check);

    srunner_run_all(sr, CK_ENV);
    nf = srunner_ntests_failed(sr);
    srunner_free(sr);

    return nf == 0 ? 0 : 1;
}

and compile with

gcc test.c -Wall -o test -lcheck -pthread -lcheck_pic -pthread -lrt -lm -lsubunit