将Googletest添加到现有CMake项目 [英] Adding Googletest To Existing CMake Project

查看:139
本文介绍了将Googletest添加到现有CMake项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having trouble integrating googletest into my existing project. I put together a simple project to represent my project's structure:

项目结构

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 /存在。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 (对不起,这个奇怪的名字,看起来我可以选择了更好的项目名称!)。

Building with CMake will create two executables, one, TestTester, and another called Test_TestTester (sorry for the odd name, it looks like I could have chosen a better project name!).

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

TestTester will be the main project executable which will run the code from existing_course/main.cpp and output sample() output = 1.

Test_TestTester 应该是main_test.cpp中的单元测试,它测试 1 == 1 1 == sample() 。这应该在项目建立时运行。

Test_TestTester should be the unit tests from main_test.cpp which tests that 1 == 1 and 1 == sample(). This should run when the project is built.

尝试:

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

I've tried using CMake's add_subdirectory() to expose a second CMakeLists.txt in the test subdirectory which has its own add_executable() with the name of the test program, however I cannot find any output related to the test program. Using enable_testing() followed by add_test() is also failing to produce any changes.

更新:

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

I realized some problems and assumptions were wrong.


  • 在CLion中,默认情况下是建立特定目标。必须调用全部生成( cmake --build ... --target all )来生成其他可执行文件。

  • 另一个我读到的与此相关的问题似乎并没有使用预编译的库。在将其包含在项目中之前,我已经在自己的计算机上编译并安装了googletest。

  • 这可能不是问题,但似乎大多数人选择在每个目录都有其目录的情况下构建项目自己的CMakeLists.txt文件。我重新组织了我的匹配项,以使遵循别人的建议更容易。

  • Within CLion, it defaults to building a particular target. Build all (cmake --build ... --target all) must be invoked to build the other executables.
  • The other questions I read related to this don't seem to be using the pre-compiled library. I compiled and installed googletest on my machine prior to its inclusion into the project.
  • This may not be a problem, but it looks like most people choose to structure their projects with each directory having its own CMakeLists.txt file. I reorganized mine to match to make following others' advice easier.

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

I updated the CMakeLists files with my changes. Using --target all builds everything appropriately, but I still can't get the tests to run when the project is built.

推荐答案

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

There's very little wrong with the specimen project you've posted.

似乎您误认为:

add_test(NAME Test COMMAND Test_TestTester)

在您的 new_test_source / CMakeLists.txt 中的

是获取 Test_TestTester
make 执行。

实际上,如 add_test 文档:

In fact, as declared by the first line of the add_test documentation:


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

Add a test to the project to be run by ctest(1).

您的 add_test 命令仅足以使 Test_TestTester 在$ b $时运行b在 make 之后,运行 ctest Test_TestTester 子项目的构建目录中。

your add_test command only suffices to get Test_TestTester run when, after make, you run ctest in the build directory of the Test_TestTester sub-project.

此外,即使这样仅当您通过调用<$中的 enable_testing()为该子项目
启用 ctest 测试时才会发生c $ c> new_test_source / CMakeLists.txt
,您不需要。您说您后来做了

Furthermore, even this will only happen if you enable ctest testing for that sub-project by invoking enable_testing() in new_test_source/CMakeLists.txt, which you don't. You say you did that later:


使用enable_testing()后跟add_test()也会产生任何变化。

Using enable_testing() followed by add_test() is also failing to produce any changes.

但这是因为您除了创建测试可以运行 ctest
仍然没有运行 ctest

But that's because you still haven't done anything but create tests that you can run with ctest, and still haven't run ctest.

因此,假设我已经在我的工作目录中找到了您的标本项目,并且我刚刚更改了
new_test_source / CMakeLists.txt 通过更改:

So assume I've got your specimen project in my working directory (which I have), and that I've just changed new_test_source/CMakeLists.txt by changing:

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

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

runs your tests. The complete test log is saved at:

$ 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-失败时输出

If you'd like to see all that on your console at the time when you run ctest, you can run it in verbose mode ctest -V. Or if you'd like only to see the details if the tests fail, ctest --output-on-failure.

一切正常,应该知道
的工作原理,也许您对此感到满意。如果 still 希望您的测试由 make 自动运行-
不是常规的-那么您需要添加对
Test_TestTester 目标的定制生成后命令,以自动运行 ctest 。只需追加,例如

Everything's working as it should, and maybe you're happy with that, knowing how it works. If you still want to your tests to be run automatically by make - which is not conventional - then you'll need to add a custom post-build command to the Test_TestTester target to run ctest automatically. Just append, e.g.

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

new_test_source / CMakeLists .txt 。那么干净的 make 的输出为:

to new_test_source/CMakeLists.txt. Then the output of a clean make is:

$ 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

这篇关于将Googletest添加到现有CMake项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆