当前位置: 首页 > news >正文

GoogleTest做单元测试

目录

  • 环境准备
  • GoogleTest

环境准备

git clone https://github.com/google/googletest.git

说cmkae版本过低了,解决方法

进到googletest中

cmake CMakeLists.txt
make
sudo make install

在这里插入图片描述


ls /usr/local/lib

存在以下文件说明安装成功

在这里插入图片描述
中间出了个问题就是,总是出现链接不成功,导致库导入不进去
在这里插入图片描述
可以对G++命令加上-L编译的命令,这样就指定了库的搜索路径。

g++ sample1.cc sample1_unittest.cc -o sample1 -L/usr/local/lib -lgtest -lgtest_main -lpthread

在这里插入图片描述

在这里插入图片描述

GoogleTest

单元测试是用来对一个模块、一个函数或者一个类来进行正确性检测的测试工作。
比如我们测试一个岛问题的解决方法

#include <iostream>
#include <initializer_list>
#include <vector>
#include <gtest/gtest.h>using namespace std;class IslandProblem {
public:using Matrix = vector<vector<char>>;IslandProblem(const initializer_list<vector<char>> list) {_islands.assign(list);}int Do() {int num = 0;for (int row = 0; row < (int)_islands.size(); row++) {for (int col = 0; col < (int)_islands[row].size(); col++) {if (canUnion(row, col)) {num++;unionIsland(row, col);}}}return num;}protected:bool canUnion(int row, int col) {if (row < 0 || row >= (int)_islands.size())return false;if (col < 0 || col >= (int)_islands[row].size())return false;if (_islands[row][col] != 1)return false;return true;}void unionIsland(int row, int col) {_islands[row][col] = 2;// upif (canUnion(row-1, col)) unionIsland(row-1, col);// leftif (canUnion(row, col-1)) unionIsland(row, col-1);// downif (canUnion(row+1, col)) unionIsland(row+1, col);// rightif (canUnion(row, col+1)) unionIsland(row, col+1);}private:Matrix _islands;
};TEST(IslandProblem, logic) {IslandProblem ip1{{1,1,1,1},{1,0,1,1},{0,0,0,0},{1,0,1,0}};EXPECT_EQ(ip1.Do(), 3);IslandProblem ip2{{1,0,1,1},{1,0,1,1},{0,0,0,0},{1,0,1,0}};EXPECT_EQ(ip2.Do(), 4);
}TEST(IslandProblem, boundary) {IslandProblem ip1{{1,1,1,1},{1,0,0,1},{1,0,0,1},{1,1,1,1}};EXPECT_EQ(ip1.Do(), 1);IslandProblem ip2{};EXPECT_EQ(ip2.Do(), 0);
}TEST(IslandProblem, exception) {IslandProblem ip1{{-1,1,1,1},{1,0,0,1},{1,0,0,1},{1,1,1,1}};EXPECT_EQ(ip1.Do(), 1);
}

解决方法要考虑:逻辑问题(功能正确),边界问题,异常情况。TEST的测试案例中已经把这些问题考虑进去了。
然后实际去测试,说明解决方法能够考虑以上三种情况在这里插入图片描述

http://www.lryc.cn/news/492358.html

相关文章:

  • 深入解析 EasyExcel 组件原理与应用
  • JSON数据转化为Excel及数据处理分析
  • (计算机网络)期末
  • 【AI技术赋能有限元分析应用实践】将FEniCS 软件安装在Ubuntu22.04
  • 快速识别模型:simple_ocr,部署教程
  • 【C/C++】数据库链接入门教程:从零开始的详细指南!MySQL集成与操作
  • C#中面试的常见问题005
  • 使用Redis生成全局唯一id
  • pnpm:包管理的新星,平替 npm 和 yarn
  • Android调起系统分享图片到其他应用
  • 详解Qt QBuffer
  • Python基础学习-11函数参数
  • GTK#框架让C# Winform程序跨平台运行
  • 在Kubernetes使用CronJob实现定时删除指定天数外的文件(我这里使用删除备份mysql数据库文件为例)
  • 使用 Elastic 收集 Windows 遥测数据:ETW Filebeat 输入简介
  • 力扣-位运算-4【算法学习day.44】
  • Stable Diffusion 3详解
  • c#异步编程(async/await)
  • TCP/IP学习笔记
  • 0000_vim自定义快捷键_alias
  • Spring Boot项目中,实体类是否需要实现Serializable接口
  • 打通工业通信壁垒实现Ethernetip转profinet网络互通
  • 数据结构_图的应用
  • C#中面试的常见问题002
  • 快速理解微服务中Ribbon的概念
  • K8S简介、使用教程
  • 极狐GitLab 17.6 正式发布几十项与 DevSecOps 相关的功能【四】
  • 麦肯锡报告 | 科技落地的真谛:超越技术本身的价值创造
  • 彻底解决 macOS 下Matplotlib 中文显示乱码问题
  • STM32-- keil 的option for target使用