[英]How to run tests and debug Google Test project in VS Code?
我想運行示例測試並調試Google 測試項目。 我在 Ubuntu 16.04 LTS 上使用 VS Code。
/home/user/Desktop/projects/cpp/googletest
本地克隆了項目,/home/user/Desktop/projects/cpp/mybuild
創建了一個名為mybuild
的新目錄。cmake -Dgtest_build_samples=ON /home/user/Desktop/projects/cpp/googletest
來構建項目這生成了一堆文件,顯然構建成功了。現在,我有兩個問題:
如何運行項目的示例測試?
如何調試這些測試和項目的源代碼?
/home/user/Desktop/projects/cpp/ # your project lives here
└─cpp/
├─ CMakeLists.txt
├─ myfunctions.h
└─ mytests.cpp
googletest
添加到此目錄:└─cpp/
├─ googletest/
├─ CMakeLists.txt
├─ myfunctions.h
└─ mytests.cpp
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
myfunctions.h
的內容:#ifndef _ADD_H
#define _ADD_H
int add(int a, int b)
{
return a + b;
}
#endif
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
接下來 go 進入build
目錄:
cd build
然后運行:
cmake ..
make
./mytests
替代方式:
CMake Tools
擴展/home/user/Desktop/projects/cpp/googletest
build/
使其如下所示:└─cpp/googletest/
├─ build/
├─ ...other googletest files
cd build
cmake -Dgtest_build_samples=ON -DCMAKE_BUILD_TYPE=Debug..
make -j4
./googletest/sample1_unittest
googletest
文件夾.vscode
目錄。 里面是settings.json
文件,打開它,添加以下內容: "cmake.configureSettings": { "gtest_build_samples": "ON" }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.