我正在尝试重写一些遗留的C代码,并希望在实际开始重写之前进行一些测试。为此我查看了CppUTest并尝试了一个示例应用程序,其中包含头文件chrtostr.h
,实现文件chrtostr.c
和名为test_chrtostr.c
的测试文件,其内容如下所示:
#include <CppUTest/CommandLineTestRunner.h>
#include "chrtostr.h"
TEST_GROUP(chrtostr)
{
}
TEST(chrtostr, test_chrtostr)
{
CHECK_EQUAL(chrtostr('n'), "sfsdfds");
}
int main(int ac, char **av)
{
return CommandLineTestRunner::RunAllTests(ac, av);
}
以及相应的Makefile.am
:
AUTOMAKE_OPTIONS = foreign
CPPUTEST_HOME = ./cpputest
CFLAGS = -g -Wall -I$(CPPUTEST_HOME)/include
LDFLAGS = -L$(CPPUTEST_HOME)/lib -lCppUTest
bin_PROGRAMS = chrtostr test_chrtostr
chrtostr_SOURCES = chrtostr.c chrtostr.h main.c
test_chrtostr_SOURCES = test_chrtostr.c
问题是,每次我尝试运行make
时,我都会得到以下追溯,这实际上对我没有多大帮助:http://pastebin.com/BK9ts3vk
答案 0 :(得分:6)
你应该从开始进行其中一个演示开始。您可以看到CppUTest如何与C一起使用。我的书“嵌入式C的测试驱动开发”将帮助您开始使用。前几章使用C-Only测试工具。后面的例子使用CppUTest(我是CppUTest的作者之一)。我还描述了C ++ C ++测试工具的优点。
詹姆斯
P.S。 - 有关CppUTest的更多信息,请查看CppUTest.org
答案 1 :(得分:2)
该测试驱动程序是用C ++编写的。您需要将其编译为C ++,因此将文件重命名为.cpp并确保调用g++
来驱动编译/链接(而不是gcc
)。
答案 2 :(得分:1)
不幸的是,CppUTest中的“HelloWorld”示例没有记录,虽然“嵌入式C的测试驱动开发”中的附录仅列出了11个条件检查,但我发现还有更多未记录的辅助函数(几乎都没有记录) )。除非你试图理解TDD的概念,否则我不会推荐CppuTest。
我会寻找更多的商业产品,或者你会收拾很多糟糕的TDD习惯,或者真的很沮丧,继续前进。
答案 3 :(得分:0)
我只是在看这个。您的代码存在一些问题。 C ++错误并不总是有助于清除它们。
我在修复之前添加了评论。
#include "CppUTest/TestHarness.h"
//The test file is c++. YOu have to tell it when you are linking to C code
extern "C"
{
#include "chrtostr.h"
}
//A test group needs to have a ';' after it. Under the hood, this macro
//create a base class for the test cases of the same name
TEST_GROUP(Chrtostr)
{
};
//CHECK_EQUAL uses ==. STRCMP_EQUAL actually compares c-strings
TEST(Chrtostr, wrong)
{
STRCMP_EQUAL(chrtostr('n'), "sfsdfds");
}