如何配置 GTest 以使用 Visual Studio Code 中的图形调试器?

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

我想要的只是能够单击 Visual Studio Code 中的可爱错误并逐步执行我的代码以找出我的 Gtest 测试失败的原因。

我不想使用这个问题中指定的子模块方法

我该怎么做?

c++ visual-studio-code googletest
1个回答
0
投票

假设目录结构为

myproject
| -- CMakeLists.txt
| -- hello_test.cpp

hello_test.cpp 看起来像:

#include <gtest/gtest.h>

TEST(hello_test, add)
{
    GTEST_ASSERT_EQ((10 + 22), 32);
}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

您的 CMakeLists.txt 应该如下所示:

cmake_minimum_required(VERSION 3.0.0)
project(myproject VERSION 0.1.0)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

include(FetchContent)

FetchContent_Declare(
  googletest
  # Specify the commit you depend on and update it regularly.
  URL https://github.com/google/googletest/archive/5376968f6948923e2411081fd9372e71a59d8e77.zip
)

FetchContent_MakeAvailable(googletest)

add_executable(hello_test hello_test.cpp)

target_link_libraries(
    hello_test
    gtest
)

include(GoogleTest)
gtest_discover_tests(hello_test)

© www.soinside.com 2019 - 2024. All rights reserved.