【问题标题】:How to run tests and debug Google Test project in VS Code?如何在 VS Code 中运行测试和调试 Google Test 项目?
【发布时间】:2020-11-04 17:10:30
【问题描述】:

我想运行示例测试并调试Google Test project。我在 Ubuntu 16.04 LTS 上使用 VS Code。

  • 我将项目克隆到本地/home/user/Desktop/projects/cpp/googletest
  • /home/user/Desktop/projects/cpp/mybuild 创建了一个名为mybuild 的新目录。
  • 根据此处的 README 说明:https://github.com/google/googletest/tree/master/googletest 我使用命令 cmake -Dgtest_build_samples=ON /home/user/Desktop/projects/cpp/googletest 构建项目,这会生成一堆文件,显然构建成功。

现在,我有两个问题:

  1. 如何运行项目的示例测试?

  2. 如何调试这些测试和项目的源代码?

【问题讨论】:

    标签: c++ visual-studio-code cmake googletest


    【解决方案1】:
    1. 从一个干净的目录开始:
    /home/user/Desktop/projects/cpp/ # your project lives here
    
    1. 添加您的 cmake 文件 (CMakeLists.txt)、源文件和测试文件。该目录现在如下所示:
    └─cpp/
        ├─ CMakeLists.txt
        ├─ myfunctions.h
        └─ mytests.cpp
    
    1. 克隆并将googletest 添加到此目录:
    └─cpp/
        ├─ googletest/
        ├─ CMakeLists.txt
        ├─ myfunctions.h
        └─ mytests.cpp
    
    1. 打开您的CMakeLists.txt 并输入以下内容:
    cmake_minimum_required(VERSION 3.12) # version can be different
    
    project(my_cpp_project) #name of your project
    
    add_subdirectory(googletest) # add googletest subdirectory
    
    include_directories(googletest/include) # this is so we can #include <gtest/gtest.h>
    
    add_executable(mytests mytests.cpp) # add this executable
    
    target_link_libraries(mytests PRIVATE gtest) # link google test to this executable
    
    1. 例如myfunctions.h的内容:
    #ifndef _ADD_H
    #define _ADD_H
    
    int add(int a, int b)
    {
        return a + b;
    }
    
    #endif
    
    1. mytests.cpp为例子的内容:
    #include <gtest/gtest.h>
    #include "myfunctions.h"
    
    TEST(myfunctions, add)
    {
        GTEST_ASSERT_EQ(add(10, 22), 32);
    }
    
    int main(int argc, char* argv[])
    {
        ::testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
    }
    

    现在您只需运行测试即可。有多种方法可以做到这一点。

    在终端中,在cpp/ 中创建一个build/ 目录:

    mkdir build
    

    您的目录现在应该如下所示:

    └─cpp/
        ├─ build/
        ├─ googletest/
        ├─ CMakeLists.txt
        ├─ myfunctions.h
        └─ mytests.cpp
    

    接下来进入build目录:

    cd build
    

    然后运行:

    cmake ..
    make
    ./mytests
    

    另一种方式:

    • 为 VS Code 安装 CMake Tools 扩展
    • 在底部栏中,您可以看到要构建/运行的当前目标(在方括号中Build [mytest]Run [mytest]):
    • 然后只需单击运行按钮。


    自行构建 Google 测试

    使用终端:

    1. 进入目录/home/user/Desktop/projects/cpp/googletest
    2. 在其中创建build/,使其如下所示:
    └─cpp/googletest/
        ├─ build/
        ├─ ...other googletest files
    
    1. cd build
    2. 运行:cmake -Dgtest_build_samples=ON -DCMAKE_BUILD_TYPE=Debug ..
    3. make -j4
    4. ./googletest/sample1_unittest

    使用 VS 代码

    1. 在 VS Code 中打开 googletest 文件夹
    2. CMake 扩展将提示配置,允许它
    3. 您将看到一个.vscode 目录。里面是settings.json 文件,打开它,添加以下内容:
        "cmake.configureSettings": { "gtest_build_samples": "ON" }
    
    1. 通过底部栏中的按钮构建和运行

    【讨论】:

    • 感谢您的回答,但我认为您误解了我的意思。我没有任何想要使用 Google Test 测试的项目。我想构建和调试 Google 测试库本身。可能吗?我该怎么做? PS:请不要删除这个答案,它很好,以后我使用谷歌测试为我的项目编写测试时会帮助我。
    • 那应该更简单,我会把它放在答案中。
    • @Waqar 当我尝试构建时,我得到 fatal error: gtest/gtest.h: No such file or directory 我使用 Using VS-Code 步骤。
    • @SachithMuhandiram 你能用命令行构建吗?您是要自己构建 GoogleTest 还是要为您的项目编写测试?
    • 如果你想将它集成到你的项目中,你必须按照帖子第一部分的说明进行操作(在构建谷歌测试本身之前)。第二部分是如果您想构建 googletest 并可能为该项目做出贡献。
    猜你喜欢
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 2019-10-10
    • 2017-10-02
    • 2019-12-29
    • 2019-05-26
    • 2020-02-06
    相关资源
    最近更新 更多