我有一个makefile,如答案中所述:
我的cpp文件中有:
#include "CppUTest/CommandLineTestRunner.h"
int main(int ac, const char** av)
{
/* These checks are here to make sure assertions outside test runs don't crash */
CHECK(true);
LONGS_EQUAL(1, 1);
return CommandLineTestRunner::RunAllTests(ac, av);
}
然后我收到错误:
undefined reference to `CommandLineTestRunner::RunAllTests(int, char const**)'
任何想法尝试什么?
答案 0 :(得分:0)
我将您的代码复制到我的一个AllTest.ccp主文件中,并且工作正常。
您可能有一个旧版本的CppUTest,它只定义了第二种形式的RunAllTests()
static int RunAllTests(int ac, const char** av);
static int RunAllTests(int ac, char** av);
我通常使用RUN_ALL_TESTS宏,并将argc定义为const char *,如下所示:
#include "CppUTest/CommandLineTestRunner.h"
int main(int ac, const char** av)
{
return RUN_ALL_TESTS(ac, av);
}
答案 1 :(得分:0)
要运行cpputest案例,您需要两个文件。一个应包含所有测试用例,另一个应仅包含main()
函数。
尝试这样的事情 -
档案:cpputest1.cpp
#include "CppUTest/TestHarness.h"
TEST_GROUP(FirstTestGroup)
{
};
TEST(FirstTestGroup, FirstTest)
{
FAIL("Fail me!");
}
TEST(FirstTestGroup, SecondTest)
{
STRCMP_EQUAL("hello", "world");
LONGS_EQUAL(1, 2);
CHECK(false);
}
文件:cpputestmain.cpp
#include "CppUTest/CommandLineTestRunner.h"
int main(int ac, char** av)
{
return CommandLineTestRunner::RunAllTests(ac, av);
}
确保这两个文件位于cpputest目录中的同一文件夹(tests
)下。并在make文件中链接这些文件夹。请浏览this site for more info
答案 2 :(得分:0)
确保文件的链接顺序正确。因此,如果要从main.o和tests.o生成可执行文件“ runtests”,则LD_LIBRARIES(请参阅CppUTest文档)应该位于最后。这样可以确保链接主程序和测试所需的符号对链接器是已知的。
runtests: main.o tests.o
g++ -o runtests main.o tests.o $(LD_LIBRARIES)