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

wkftcu5l  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(176)

我所希望的只是能够单击Visual Studio代码中的可爱bug,并逐步通过我的代码来找出为什么我的Gtest测试失败。
我不想使用submodule approach specified in this question
我该怎么做呢?

niwlg2el

niwlg2el1#

假设目录结构为

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)

相关问题