【问题标题】:Adding Googletest To Existing CMake Project将 Googletest 添加到现有的 CMake 项目
【发布时间】:2019-02-12 16:59:51
【问题描述】:

我无法将 googletest 集成到我现有的项目中。我整理了一个简单的项目来代表我的项目结构:

项目结构

CMakeLists.txt:

cmake_minimum_required(VERSION 3.13)
project(TestTester)
set(CMAKE_CXX_STANDARD 14)

include_directories(existing_source)
add_subdirectory(existing_source)
add_subdirectory(new_test_source)

existing_source/CMakeLists.txt:

cmake_minimum_required(VERSION 3.13)
project(Test_TestTester)
set(CMAKE_CXX_STANDARD 14)

add_executable(TestTester main.cpp existing.h)

new_test_source/CMakeLists.txt:

cmake_minimum_required(VERSION 3.13)
project(Test_TestTester)
set(CMAKE_CXX_STANDARD 14)

find_package(PkgConfig REQUIRED)
pkg_check_modules(gtest REQUIRED gtest>=1.8.1)

SET(CMAKE_CXX_FLAGS -pthread)
enable_testing()

include_directories(${gtest_INCLUDE_DIRS})

add_executable(Test_TestTester main_test.cpp ../existing_source/existing.h)
target_link_libraries(Test_TestTester ${gtest_LIBRARIES})
add_test(NAME Test COMMAND Test_TestTester)

existing_source/existing.h

#ifndef TESTTESTER_EXISTING_H
#define TESTTESTER_EXISTING_H

int sample() {
    return 1;
}

#endif //TESTTESTER_EXISTING_H

existing_source/main.cpp

#include <iostream>
#include "existing.h"

int main() {
    std::cout << "sample() output = " << sample() << std::endl;
    return 0;
}

new_test_source/main_test.cpp

#include <gtest/gtest.h>
#include "../existing_source/existing.h"

TEST(SampleTestCase, TestOneIsOne) {
    EXPECT_EQ(1, 1);
}

TEST(ExistingCodeTestCase, TestSample) {
    EXPECT_EQ(1, sample());
}


GTEST_API_ int main(int argc, char **argv) {
    printf("Running main() from %s\n", __FILE__);
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

目标:

使用 CMake 构建将创建两个可执行文件,一个是 TestTester,另一个是 Test_TestTester(抱歉,名字有点奇怪,看来我可以选择一个更好的项目名字!)。

TestTester 将是主项目可执行文件,它将运行 existing_course/main.cpp 中的代码并输出 sample() output = 1

Test_TestTester 应该是来自 main_test.cpp 的单元测试,用于测试 1 == 11 == sample()。这应该在构建项目时运行。

尝试:

我尝试使用 CMake 的 add_subdirectory() 在测试子目录中公开第二个 CMakeLists.txt,该子目录有自己的 add_executable() 和测试的名称程序,但是我找不到与测试程序相关的任何输出。使用 enable_testing() 后跟 add_test() 也无法产生任何更改。

更新:

我意识到一些问题和假设是错误的。

  • 在 CLion 中,它默认构建特定目标。必须调用 Build all (cmake --build ... --target all) 来构建其他可执行文件。
  • 我阅读的与此相关的其他问题似乎没有使用预编译库。在将 googletest 纳入项目之前,我在我的机器上编译并安装了它。
  • 这可能不是问题,但看起来大多数人选择使用每个目录都有自己的 CMakeLists.txt 文件来构建他们的项目。我重新组织了我的匹配,以便更容易遵循他人的建议。

我用我的更改更新了 CMakeLists 文件。使用--target all 可以正确构建所有内容,但是在构建项目时我仍然无法运行测试。

【问题讨论】:

  • get the tests to run 什么意思?如何?有错误信息吗?你试过什么?你如何运行它们?测试可执行文件是否正确构建?如果您自己运行测试可执行文件怎么办?为什么要在 .h 文件中提供具有外部链接的符号的定义? sample 函数不应该至少有staticstatic inline 吗?
  • 测试通过执行 Test_TestTester 可执行文件运行。抱歉,如果我不清楚,我的意思是此时的挂断是将测试的执行集成到构建过程中。 add_test() 的 COMMAND 部分是告诉 CMake 执行什么命令来运行测试,对吗?静态或内联说明符可能更正确,但这只是一个有效的最小示例。

标签: cmake c++14 googletest


【解决方案1】:

您发布的样本项目几乎没有错误。

您似乎错误地认为:

add_test(NAME Test COMMAND Test_TestTester)

在您的new_test_source/CMakeLists.txt 中获取您的Test_TestTestermake执行。

其实正如the add_test documentation的第一行所声明的那样:

在项目中添加一个测试,由 ctest(1) 运行。

您的add_test 命令仅足以让Test_TestTester 运行时, 在make之后,在Test_TestTester子项目的build目录下运行ctest

此外,只有在您为该子项目启用ctest 测试时才会发生这种情况 通过在new_test_source/CMakeLists.txt 中调用enable_testing(),而您没有这样做。你说你做到了 后来:

使用 enable_testing() 后跟 add_test() 也无法产生任何更改。

但那是因为除了创建可以运行的测试您还没有做任何事情ctest, 仍然没有运行ctest

所以假设我的工作目录中有你的样本项目(我有),并且 我刚刚通过更改更改了new_test_source/CMakeLists.txt

project(Test_TestTester)

到:

project(Test_TestTester)
enable_testing()

然后:

$ mkdir build
$ cd build
$ cmake ..

生成构建系统,并且:

$ make
Scanning dependencies of target TestTester
[ 25%] Building CXX object existing_source/CMakeFiles/TestTester.dir/main.cpp.o
[ 50%] Linking CXX executable TestTester
[ 50%] Built target TestTester
Scanning dependencies of target Test_TestTester
[ 75%] Building CXX object new_test_source/CMakeFiles/Test_TestTester.dir/main_test.cpp.o
[100%] Linking CXX executable Test_TestTester
[100%] Built target Test_TestTester

构建一切,并且:

# We're still in `./build`
$ cd new_test_source/
$ ctest
Test project /home/imk/develop/so/scrap2/build/new_test_source
    Start 1: Test
1/1 Test #1: Test .............................   Passed    0.00 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.00 sec

运行您的测试。完整的测试日志保存在:

$ cat ./Testing/Temporary/LastTest.log
Start testing: Feb 12 19:23 GMT
----------------------------------------------------------
1/1 Testing: Test
1/1 Test: Test
Command: "/home/imk/develop/so/scrap2/build/new_test_source/Test_TestTester"
Directory: /home/imk/develop/so/scrap2/build/new_test_source
"Test" start time: Feb 12 19:23 GMT
Output:
----------------------------------------------------------
Running main() from /home/imk/develop/so/scrap2/new_test_source/main_test.cpp
[==========] Running 2 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 1 test from SampleTestCase
[ RUN      ] SampleTestCase.TestOneIsOne
[       OK ] SampleTestCase.TestOneIsOne (0 ms)
[----------] 1 test from SampleTestCase (0 ms total)

[----------] 1 test from ExistingCodeTestCase
[ RUN      ] ExistingCodeTestCase.TestSample
[       OK ] ExistingCodeTestCase.TestSample (0 ms)
[----------] 1 test from ExistingCodeTestCase (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 2 test suites ran. (0 ms total)
[  PASSED  ] 2 tests.
<end of output>
Test time =   0.00 sec
----------------------------------------------------------
Test Passed.
"Test" end time: Feb 12 19:23 GMT
"Test" time elapsed: 00:00:00
----------------------------------------------------------

End testing: Feb 12 19:23 GMT

如果您想在运行 ctest 时在控制台上看到所有内容,可以运行 它处于详细模式ctest -V。或者,如果您只想查看详细信息,如果 测试失败,ctest --output-on-failure

一切正常,也许您对此感到满意,知道如何 有用。如果您仍然希望您的测试由 make 自动运行 - 这 不是传统的 - 那么您需要将自定义的构建后命令添加到 Test_TestTester 目标自动运行 ctest。只需附加,例如

add_custom_command(TARGET Test_TestTester
                   COMMENT "Run tests"
                   POST_BUILD COMMAND ctest ARGS --output-on-failure
)

new_test_source/CMakeLists.txt。那么一个干净的make 的输出是:

$ make
Scanning dependencies of target TestTester
[ 25%] Building CXX object existing_source/CMakeFiles/TestTester.dir/main.cpp.o
[ 50%] Linking CXX executable TestTester
[ 50%] Built target TestTester
Scanning dependencies of target Test_TestTester
[ 75%] Building CXX object new_test_source/CMakeFiles/Test_TestTester.dir/main_test.cpp.o
[100%] Linking CXX executable Test_TestTester
Run tests
Test project /home/imk/develop/so/scrap2/build/new_test_source
    Start 1: Test
1/1 Test #1: Test .............................   Passed    0.00 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.00 sec
[100%] Built target Test_TestTester

【讨论】:

    猜你喜欢
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    相关资源
    最近更新 更多