我想跟随官方Check教程学习,但需要具备Autotools的工作知识,而我并没有。我希望只是写几个快速测试,但我发现这个教程很无从下手。它依赖于Autoconf、Automake和一些Check宏中的许多魔法。它没有解释Check实际上是如何工作的,以便我可以手动构建测试。
我如何在没有Autotools的情况下使用Check?
我想跟随官方Check教程学习,但需要具备Autotools的工作知识,而我并没有。我希望只是写几个快速测试,但我发现这个教程很无从下手。它依赖于Autoconf、Automake和一些Check宏中的许多魔法。它没有解释Check实际上是如何工作的,以便我可以手动构建测试。
我如何在没有Autotools的情况下使用Check?
#include "implementation.h"
int sum(int a, int b) {
return a + b;
}
#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");
将文件保存为implementation-test.check(您可以选择任何名称/扩展名,但如果要遵循我的指南,请保持这些名称),然后运行附带Check的awk脚本。 您甚至不必费心编写检查框架的样板代码!(有关更多详细信息,请参见man checkmk)
checkmk implementation-test.check >implementation-test.c
/*
* 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;
}
gcc -Wall -o sum2ints-test implementation.c implementation-test.c -lcheck
./sum2ints
make -f sum2ints.makefile
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
LIBS=-lcheck -lm -lpthread -lrt
。 - Mateusz Charytoniukpkg-config --cflags --libs check
来确定需要添加的附加内容,该命令可能会返回以下内容:-pthread -pthread -lcheck_pic -lrt -lm
。请使用此整个字符串代替原作者提供的-lcheck
。 - skelliam@freestyler的答案很好,但仍然使用checkmk
,这是不必要的。
这是一个最小化的示例,不使用checkmk
。
将以下内容放入名为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;
}
并编译使用
gcc test.c -Wall -o test -lcheck -pthread -lcheck_pic -pthread -lrt -lm -lsubunit
ld: library not found for -lcheck_pic clang: error: linker command failed with exit code 1 (use -v to see invocation)
。您知道可能出了什么问题吗? - ecjb