将Googletest添加到现有CMake项目

问题描述 投票:3回答:1

我在将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()在test子目录中公开第二个CMakeLists.txt,它有自己的add_executable()和测试程序的名称,但是我找不到任何与测试程序相关的输出。使用enable_testing()后跟add_test()也无法产生任何更改。

更新:

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

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

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

cmake c++14 googletest
1个回答
2
投票

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

你似乎错误地认为:

add_test(NAME Test COMMAND Test_TestTester)

在你的new_test_source/CMakeLists.txt中,只需要让Test_TestTester执行你的make

事实上,正如the add_test documentation第一行宣布的那样:

将测试添加到要由ctest(1)运行的项目中。

你的add_test命令只需要在Test_TestTester之后运行make,在ctest子项目的构建目录中运行Test_TestTester

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

使用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
© www.soinside.com 2019 - 2024. All rights reserved.