2

我想测试我的 C 代码并决定使用 check 作为测试框架。但我不明白如何编译代码?在本教程中,他们已经有非常大的 makefile,但他们没有解释如何构建它们或我需要什么 gcc 标志。

如何执行我的简单测试?

4

4 回答 4

1

使用自动工具非常简单。在configure.ac中,您检查目标系统上是否存在 Check 单元测试框架:

PKG_CHECK_MODULES([CHECK],[check >= 0.8.2],[have_check="yes"],
  AC_MSG_WARN(['Check' unit testing framework not found. It would be impossible to run unit tests!"])
  [have_check="no"])

Makefile.am中,您指定哪些文件以及如何编译以构建单元测试:

if HAVE_CHECK
check_PROGRAMS = bin/some_unit_tests 
bin_some_unit_tests_SOURCES = source1.c source2.c ...
bin_some_unit_tests_CFLAGS = -g -pg -O2 -fprofile-arcs -ftest-coverage ...
bin_some_unit_tests_LDFLAGS = -g -pg -no-install
bin_some_unit_tests_LDADD = @CHECK_LIBS@
TESTS = bin/some_unit_tests
TESTS_ENVIRONMENT = CK_FORK=yes
CK_VERBOSITY = verbose
CLEANFILES = some_unit_tests.log
endif

然后通过发出以下命令运行单元测试:

make check

通过使用 -pg 标志,您将能够从执行单元测试中获取分析信息。

于 2012-02-29T17:10:48.457 回答
0

您可以在“configure->make->make install”之后获得一个名为“libcheck.a”的存档文件。通常libcheck.a 会被安装到“/usr/lib”或“/usr/local/lib”,Gcc 会自动找到libcheck.a 的位置。您需要做的是在编译命令行中添加 -lcheck 选项,例如“gcc -o test_add test_add.c -lcheck”。

除了 check,还有很多其他的 c 单元测试框架,例如lcutcmockery

于 2012-02-29T16:11:54.887 回答
0

There are open source projects that use check for unit testing. One example is TORQUE. You can check out the source using svn. Currently you'd want the trunk to see unit tests - svn co svn://svn.clusterresources.com/torque/trunk

Look at the directory src/lib/Libutils/test/resizable_array for one example of how to set things up. Like bsa2000's answer says, there's a lot of setup in terms of altering makefiles.

于 2012-02-29T17:13:15.817 回答
0

我个人对Check不熟悉。

我会推荐使用CppUTest并阅读TDD For Embedded C。它有很好的解释,适用于 C 和 C++。

另一个选择是 Unity,也记录在参考书中。

于 2012-02-29T15:01:00.337 回答