I am trying to follow along with the official Check tutorial, but it requires a working knowledge of Autotools, which I don't have. I was hoping just to write a couple quick tests, and I'm finding this tutorial overwhelming. It relies on a lot of magic in Autoconf, Automake, and some Check macros. It doesn't explain how Check actually works so that I could build tests by hand.
How can I use Check without Autotools?
The most scalable way to write unit tests in C is using a unit testing framework, such as: CppUTest. Unity. Google Test.
Skip the decorated test unless condition is true. Mark the test as an expected failure. If the test fails when run, the test is not counted as a failure. The following example demonstrates the use of conditional skipping and expected failure.
Avoid Test Interdependence You, therefore, cannot count on the test suite or the class that you're testing to maintain state in between tests. But that won't always make itself obvious to you. If you have two tests, for instance, the test runner may happen to execute them in the same order each time.
Unit testing can be done manually but is usually automated. Unit testing is a part of the test-driven development (TDD) methodology that requires developers to first write failing unit tests. Then they write code in order to change the application until the test passes.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With