$aptitude show checkPackage: check State: not installed Version: 0.9.4-3 Priority: optional Section: devel Maintainer: Robert Lemmen <robertle@semistable.com> Uncompressed Size: 332k Description: unit test framework for C Check features a simple interface for defining unit tests, putting little in the way of the developer. Tests are run in a separate address space, so Check can catch both assertion failures and code errors that cause segmentation faults or other signals. The output from unit tests can be used within source code editors and IDEs.
Do Makefile přidáme:
TCASES = check_open.o
CFLAGS += -ansi -pedantic -Wall
LDFLAGS += -lcheck
all: test
clean:
	rm -f *.o *~ $(TESTS)
test: run-tests
        @./run-tests
run-tests: run-tests.o $(TCASES)
        $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) -lcheckSpouštění testů
Příklad 87.1. run-tests.c
/* run-tests.c */
#include <stdlib.h>
#include "tests.h"
/*
 * Create Test Suite.
 */
Suite *api_suite(void)
{
        Suite *aSuite = suite_create("API");
	/* přidáváme postupně jednotlivé testy */
        suite_add_tcase (aSuite, tc_open());
        return aSuite;
}
/*
 * MAIN
 */
int
main(void)
{
        int nf;
        Suite *s = api_suite();
	SRunner *sr = srunner_create(s);
        srunner_run_all(sr, CK_NORMAL);
        nf = srunner_ntests_failed(sr);
        srunner_free(sr);
        return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}tests.h
#include <check.h> /* jednotlivé testy */ TCase *tc_open(void)
Samotný test je naprogramován v samostatném souboru check_open.c
#include "tests.h"
#include <fcntl.h>
…
/*
 *
 */
START_TEST(test_create_file) {
        int fd;
        char msg[] = "File contents.\n";
        fd = open(FILENAME, O_WRONLY|O_CREAT, 0640);
        fail_if(fd < 0, "Can't create file:" FILENAME);
        if (write(fd, msg, strlen(msg)) < 0, "Can't write to file.");
} END_TEST
TCase *tc_open()
{
        TCase *aTCase = tcase_create("open()");
        /* Add all the tests to aTCase */
        tcase_add_test(aTCase, test_create_file);
        return aTCase;
}