cmake - 将 Googletest 添加到现有的 CMake 项目

标签 cmake c++14 googletest

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

项目结构

Project Structure Image (Can't post directly without 10 rep)

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)

现有源/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.h
#ifndef TESTTESTER_EXISTING_H
#define TESTTESTER_EXISTING_H

int sample() {
    return 1;
}

#endif //TESTTESTER_EXISTING_H

现有源/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适本地构建所有东西,但是在构建项目时我仍然无法运行测试。

    最佳答案

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

    您似乎错误地认为:

    add_test(NAME Test COMMAND Test_TestTester)
    

    在您的 new_test_source/CMakeLists.txt只需要得到您的Test_TestTestermake 执行.

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

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



    您的 add_test命令仅足以获取 Test_TestTester运行时,
    make 之后, 你运行 ctest Test_TestTester 的构建目录中子项目。

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

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



    但那是因为除了创建可以使用 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
    

    关于cmake - 将 Googletest 添加到现有的 CMake 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54655117/

    相关文章:

    c++ - 如何启用帧指针?

    c++ - 如何让 cpack 生成正确的开始菜单快捷方式

    c++ - memcpy 可以用于 std::aligned_storage 吗?

    c++ - 接受类型或模板作为参数的模板

    c++ - 设置 Google Test 时找不到 -lgtest

    c - 正确设置cmakelist来编译项目

    c++ - Boost.Test 和 CTest 没有为参数 color_output 提供参数

    c++ - 选择正确功能的更好方法?

    c++ - 如何使用谷歌测试测试一些代码?

    c++ - 将测试从 GoogleTest 升级到 GoogleMock (Ubuntu 14) 时出现与 pthread 相关的错误