Stu*_*Man 9 c++ cmake googletest visual-studio-code
我想运行示例测试并调试Google 测试项目。我在 Ubuntu 16.04 LTS 上使用 VS Code。
/home/user/Desktop/projects/cpp/googletest,mybuildat的新目录/home/user/Desktop/projects/cpp/mybuild。cmake -Dgtest_build_samples=ON /home/user/Desktop/projects/cpp/googletest来构建项目,这生成了一堆文件,显然构建成功。现在,我有两个问题:
如何为项目运行示例测试?
我如何调试这些测试和项目的源代码?
Waq*_*med 25
/home/user/Desktop/projects/cpp/ # your project lives here
Run Code Online (Sandbox Code Playgroud)
??cpp/
?? CMakeLists.txt
?? myfunctions.h
?? mytests.cpp
Run Code Online (Sandbox Code Playgroud)
googletest到此目录:??cpp/
?? googletest/
?? CMakeLists.txt
?? myfunctions.h
?? mytests.cpp
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
myfunctions.h的例子:#ifndef _ADD_H
#define _ADD_H
int add(int a, int b)
{
return a + b;
}
#endif
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
现在你只需要运行测试。有多种方法可以做到这一点。
在终端中,在以下位置创建一个build/目录cpp/:
mkdir build
Run Code Online (Sandbox Code Playgroud)
您的目录现在应如下所示:
??cpp/
?? build/
?? googletest/
?? CMakeLists.txt
?? myfunctions.h
?? mytests.cpp
Run Code Online (Sandbox Code Playgroud)
接下来进入build目录:
cd build
Run Code Online (Sandbox Code Playgroud)
然后运行:
cmake ..
make
./mytests
Run Code Online (Sandbox Code Playgroud)
替代方式:
CMake ToolsVS Code的扩展/home/user/Desktop/projects/cpp/googletestbuild/在其中创建,使其看起来如下所示:??cpp/googletest/
?? build/
?? ...other googletest files
Run Code Online (Sandbox Code Playgroud)
cd buildcmake -Dgtest_build_samples=ON -DCMAKE_BUILD_TYPE=Debug ..make -j4./googletest/sample1_unittestgoogletestVS Code 中打开文件夹.vscode目录。里面是settings.json文件,打开它,然后添加以下内容: "cmake.configureSettings": { "gtest_build_samples": "ON" }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11623 次 |
| 最近记录: |