GTest + vscode 实现 C++ 单元测试

GTest + vscode 实现 C++ 单元测试

本文将介绍如何以源码的形式在 vscode 中使用 googletest,重点在于目录结构与 CMakeLists.txt 的创建,不涉及如何在 vscode 中使用 CMake 进行构建与生成。目标是将生产代码与测试代码分离,保持代码环境整洁。

创建项目 gtestVscodeEx,目录结构如下:

gtestVscodeEx
│   CMakeLists.txt    
│
└───include
│   │   test.h
│   
└───src
│   │   main.cpp
│   │   test.cpp
│   
└───tests
│	│	googletest-master
│	│	CMakeLists.txt
│	│	test0.cpp

上述 includesrc 中为生产代码,tests 中为测试代码与 gtest 源码。

其中,test.h 内容如下:

#include <iostream>

class TestClass
{
private:
    int var;
public:
    TestClass();
    ~TestClass();
    int GetVar();
};

test.cpp 内容如下:

#include "test.h"

TestClass::TestClass()
{
    var = 1;
}

TestClass::~TestClass()
{
}

int TestClass::GetVar()
{
    return 
<think>我们被要求提供一个在VSCode上开始进行第一个简单的GTest+CTest的方法。根据引用[1][2][3][4],我们知道: - GTest是Google的C++测试框架 - CTest是CMake的测试工具,可以集成GTest测试 - 在CMake项目中配置GTest和CTest可以实现自动化测试 下面是详细步骤: 1. **安装必要工具**: - 确保已安装VSCode、CMake(至少3.9版本,引用[2])、GTest(可以通过包管理器安装或使用FetchContent下载,引用[3]) 2. **创建项目结构**: - 创建一个新的文件夹作为项目根目录,例如`gtest_demo` - 在该目录下创建`src`和`test`子目录,分别存放源代码和测试代码 3. **编写示例代码**: - 在`src`目录下创建一个简单的库,例如`math_functions.h`和`math_functions.cpp`,内容如下: **math_functions.h**: ```cpp #ifndef MATH_FUNCTIONS_H #define MATH_FUNCTIONS_H int add(int a, int b); #endif ``` **math_functions.cpp**: ```cpp #include "math_functions.h" int add(int a, int b) { return a + b; } ``` - 在`test`目录下创建测试文件`test_math_functions.cpp`: ```cpp #include "math_functions.h" #include <gtest/gtest.h> TEST(AddTest, PositiveNumbers) { EXPECT_EQ(add(1, 2), 3); } TEST(AddTest, NegativeNumbers) { EXPECT_EQ(add(-1, -2), -3); } ``` 4. **配置CMakeLists.txt**: - 在项目根目录创建`CMakeLists.txt`,内容如下: ```cmake cmake_minimum_required(VERSION 3.9) project(GTestDemo) # 设置C++标准 set(CMAKE_CXX_STANDARD 11) # 添加可执行文件(主程序,可选) add_executable(main src/main.cpp) # 如果没有main函数,可以省略 # 添加库 add_library(math_functions src/math_functions.cpp) # 查找GTest,如果找不到则自动下载(引用[3]) cmake_policy(SET CMP0135 NEW) find_package(GTest) if(NOT GTest_FOUND) message("GTest not found, download it...") include(FetchContent) FetchContent_Declare( googletest URL https://github.com/google/googletest/archive/refs/heads/main.zip ) FetchContent_MakeAvailable(googletest) endif() # 创建测试可执行文件 add_executable(test_math_functions test/test_math_functions.cpp) target_link_libraries(test_math_functions math_functions GTest::gtest_main) # 开启测试 enable_testing() add_test(NAME math_test COMMAND test_math_functions) ``` 5. **在VSCode中配置**: - 打开项目文件夹 - 安装CMake Tools扩展 - 按`Ctrl+Shift+P`,执行`CMake: Configure`,选择编译器(如GCC或Clang) - 配置完成后,CMake Tools会生成构建文件 6. **构建和运行测试**: - 在VSCode底部状态栏,点击`Build`按钮(或执行`CMake: Build`命令) - 构建成功后,执行测试: - 方法1:在终端运行`ctest` - 方法2:在VSCode中,按`Ctrl+Shift+P`,执行`CMake: Run Tests`(需要CMake Tools扩展支持) - 方法3:直接运行生成的可执行文件`./test_math_functions` 7. **查看测试结果**: - 如果使用`ctest`,终端会显示测试结果: ``` Test project /path/to/build Start 1: math_test 1/1 Test #1: math_test ...................... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - 运行可执行文件会输出GTest的详细结果 8. **在VSCode中调试测试**: - 打开测试文件`test_math_functions.cpp` - 在测试用例旁点击`Run Test`或`Debug Test`(需要安装C/C++扩展) > **注意**:如果使用自动下载GTest的方式,首次配置可能需要较长时间下载。 通过以上步骤,我们实现了在VSCode中使用GTest和CTest进行自动化单元测试。CTest会管理测试的运行,而GTest提供测试框架[^1][^2][^3][^4]。 ```mermaid graph TD A[安装工具] --> B[创建项目结构] B --> C[编写代码] C --> D[配置CMakeLists.txt] D --> E[VSCode配置] E --> F[构建] F --> G[运行测试] G --> H[查看结果] ``` §§相关问题§§ 1. 如何为CTest添加多个测试用例? 2. 如何在VSCode中调试GTest测试用例? 3. 如何配置GTest以生成XML测试报告? 4. CMake中如何管理多个测试目标? </think> 以下是在VSCode中配置第一个简单GTest+CTest项目的完整步骤: ### 1. 环境准备 1. **安装必要组件**: ```bash # Ubuntu示例 sudo apt install cmake g++ libgtest-dev ``` 2. **VSCode扩展**: - C/C++ (Microsoft) - CMake Tools (Microsoft) - Test Explorer UI (适配CTest)[^1][^2] ### 2. 项目结构 ``` my_project/ ├── CMakeLists.txt ├── src/ │ └── math_functions.cpp ├── include/ │ └── math_functions.h └── test/ └── test_math.cpp ``` ### 3. 文件内容 **include/math_functions.h**: ```cpp #pragma once int add(int a, int b); ``` **src/math_functions.cpp**: ```cpp #include "math_functions.h" int add(int a, int b) { return a + b; } ``` **test/test_math.cpp**: ```cpp #include "math_functions.h" #include <gtest/gtest.h> TEST(MathTest, AddTest) { EXPECT_EQ(add(2, 3), 5); EXPECT_EQ(add(-1, 1), 0); } ``` ### 4. CMake配置(核心步骤) **CMakeLists.txt**: ```cmake cmake_minimum_required(VERSION 3.10) project(GTestDemo) # 自动下载GTest(无需预先安装) include(FetchContent) FetchContent_Declare( googletest URL https://github.com/google/googletest/archive/refs/heads/main.zip ) FetchContent_MakeAvailable(googletest) # 引用[3] # 添加主库 add_library(math_functions src/math_functions.cpp ) target_include_directories(math_functions PUBLIC include) # 添加测试 add_executable(test_math test/test_math.cpp) target_link_libraries(test_math math_functions gtest_main ) # 启用CTest enable_testing() add_test(NAME MathTest COMMAND test_math) # 引用[2] ``` ### 5. VSCode操作步骤 1. 打开项目文件夹 2. 按`Ctrl+Shift+P`执行: ``` CMake: Configure CMake: Build ``` 3. 运行测试: - **方法1**:在底部状态栏点击`[Build]`旁的下拉菜单 → 选择`test_math` - **方法2**:打开测试文件 → 点击`Run Test`/`Debug Test`代码透镜 - **方法3**:终端执行 `ctest -V`[^2] ### 6. 测试结果验证 成功输出示例: ``` [==========] Running 1 test from 1 test suite [ RUN ] MathTest.AddTest [ OK ] MathTest.AddTest (0 ms) [==========] 1 test from 1 test suite ran (1 ms total) ``` ### 7. 常见问题解决 1. **GTest找不到**:确保`FetchContent`配置正确(引用[3]) 2. **链接错误**:检查`target_link_libraries`是否包含`gtest_main` 3. **CTest不显示**: ```json // settings.json "cmake.testExplorer.useCMakeTests": true ``` > **提示**:在测试文件中使用`TEST_F`可创建测试夹具,构建更复杂的测试场景[^4]。 ```mermaid graph LR A[创建CMake项目] --> B[编写被测试代码] B --> C[编写GTest用例] C --> D[配置CMakeLists] D --> E[FetchContent引入GTest] E --> F[add_test注册CTest] F --> G[VSCode构建运行] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值