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
上述 include 与 src 中为生产代码,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

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



