Google Test简述

article/2025/11/7 12:58:28

Google Test简述

Google Test 是由 Google 推出的 C++ 测试框架,具有稳定、跨平台、快速等特点,能够为 C++ 开发人员提供有效的单元测试和集成测试工具。Google Test 支持 Linux、macOS 和 Windows 操作系统,同时支持 gcc、clang 和 MSVC 等多种编译器和构建系统。Google Test 还提供了大量的失败信息,方便开发人员排查问题。

特点

  • 跨平台
  • 不止单元测试
  • 稳定并且可重复
  • 提供足够的失败信息
  • 快速
  • 线程安全

支持的平台,编译器和构建系统
平台

  • Linux
  • macOS
  • Windows

编译器

  • gcc 5.0+
  • clang 5.0+
  • MSVC 2015+
    macOS users: Xcode 9.3+ provides clang 5.0+.

构建系统

  • Bazel
  • CMake

1.最简单的使用方法-cmake通过联网编译

注意此方法必须要联网的,因为是cmake直接从github上面拉取了googletest项目。
我是在mac os上测试的。
首先,我们需要创建一个名为 hello_test.cc 的文件,并在其中添加以下代码:

#include <gtest/gtest.h>// Demonstrate some basic assertions.
TEST(HelloTest, BasicAssertions) {// Expect two strings not to be equal.EXPECT_STRNE("hello", "world");// Expect equality.EXPECT_EQ(7 * 6, 42);
}

然后,我们需要创建一个名为 CMakeLists.txt 的文件,并在其中添加以下代码:

cmake_minimum_required(VERSION 3.14)
project(my_project)# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)include(FetchContent)
FetchContent_Declare(googletestGIT_REPOSITORY https://github.com/google/googletest.gitGIT_TAG release-1.12.1
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()add_executable(hello_testhello_test.cc
)
target_link_libraries(hello_testGTest::gtest_main
)include(GoogleTest)
gtest_discover_tests(hello_test)

然后在当前目录执行以下命令,进行 CMake 的配置:

cmake -S . -B build

在这里插入图片描述
执行以上命令后,会在当前目录下创建一个名为 build 的文件夹,其中包含了 CMake 生成的 Makefile 和其他必要的文件。

接下来,我们需要执行以下命令,进行编译和链接:
cmake --build build
这一步会生成可执行文件
请添加图片描述
最后,我们需要执行以下命令,执行测试:
cd build && ctest
请添加图片描述
到这里已经搭建了一个很简单的googletest程序

从这里可以学会cmake的搭建一个应用的步骤

cmake -S . -B build # 搭建项目到build目录
cmake --build build # 编译链接为二进制程序
cd build #然后可以执行程序了

2.源代码编译

Google Test不是一个header-only的库,所以需要编译,同时确保头文件在包含目录中。
本文讲诉使用cmake构建Google Test,如果没有下载源代码需要下载。
Google Test使用cmake构建,cmake是现代许多C/C++项目的构建器,掌握了是很有好处的,cmake相当于对MakeFile的封装,其实底层还是调用的gcc/g++等编译器。

使用cmake编译:

git clone https://github.com/google/googletest.git -b release-1.10.0 #拉取源代码
cd googletest # 进入源代码目录
mkdir build # 创建 build 目录
cd build # 进入 build 目录
cmake .. # 生成构建文件
make # 编译生成库文件

如果接着输入如下代码,就可以直接在build目录生成库了,-S是source的缩写代表源码路径,-B是build的缩写,代表生成路径

cmake -S . -B build

如果要编译为动态库,则

cmake -DGTEST_CREATE_SHARED_LIBRARY=1 -S . -B build

或者进入build打开sln文件即可用IDE编译。
库文件生成在build/lib目录下面,默认为静态库。头文件在googletest/include目录下面。
我这里是使用Xcode打开,
在这里插入图片描述
点击这个按钮就生成了文件
在这里插入图片描述
可以看到库文件已经生成
在这里插入图片描述

生成了库和头文件之后,就可以使用googletest了,windows平台下类似操作,使用Visual Studio生成项目文件,然后去目录中找到对应的头文件与库文件,最后将要测试的项目来编译就行了。

3. VS2019新建项目

还有一种windows上可以用的,在vs2019,我发现googletest是默认的项目模板之一,所以直接使用Visual Studio2019新建一个Google Test项目。如下图
在这里插入图片描述

测试代码示例

现有如下文件

sample1.h

#ifndef GOOGLETEST_SAMPLES_SAMPLE1_H_
#define GOOGLETEST_SAMPLES_SAMPLE1_H_// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
int Factorial(int n);
#endif  // GOOGLETEST_SAMPLES_SAMPLE1_H_

sample2.cpp

#include "sample1.h"// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
int Factorial(int n) {int result = 1;for (int i = 1; i <= n; i++) {result *= i;}return result;
}

sample1_unittest.cpp

// Step 1. Include necessary header files such that the stuff your
// test logic needs is declared.
//
// Don't forget gtest.h, which declares the testing framework.
#include <limits.h>
#include "sample1.h"
#include "gtest/gtest.h"
namespace {// Step 2. Use the TEST macro to define your tests.
//
// TEST has two parameters: the test case name and the test name.
// After using the macro, you should define your test logic between a
// pair of braces.  You can use a bunch of macros to indicate the
// success or failure of a test.  EXPECT_TRUE and EXPECT_EQ are
// examples of such macros.  For a complete list, see gtest.h.
//
// <TechnicalDetails>
//
// In Google Test, tests are grouped into test cases.  This is how we
// keep test code organized.  You should put logically related tests
// into the same test case.
//
// The test case name and the test name should both be valid C++
// identifiers.  And you should not use underscore (_) in the names.
//
// Google Test guarantees that each test you define is run exactly
// once, but it makes no guarantee on the order the tests are
// executed.  Therefore, you should write your tests in such a way
// that their results don't depend on their order.
//
// </TechnicalDetails>// Tests Factorial().// Tests factorial of negative numbers.
TEST(FactorialTest, Negative) {// This test is named "Negative", and belongs to the "FactorialTest"// test case.EXPECT_EQ(1, Factorial(-5));EXPECT_EQ(1, Factorial(-1));EXPECT_GT(Factorial(-10), 0);// <TechnicalDetails>//// EXPECT_EQ(expected, actual) is the same as////   EXPECT_TRUE((expected) == (actual))//// except that it will print both the expected value and the actual// value when the assertion fails.  This is very helpful for// debugging.  Therefore in this case EXPECT_EQ is preferred.//// On the other hand, EXPECT_TRUE accepts any Boolean expression,// and is thus more general.//// </TechnicalDetails>
}// Tests factorial of 0.
TEST(FactorialTest, Zero) {EXPECT_EQ(1, Factorial(0));
}// Tests factorial of positive numbers.
TEST(FactorialTest, Positive) {EXPECT_EQ(1, Factorial(1));EXPECT_EQ(2, Factorial(2));EXPECT_EQ(6, Factorial(3));EXPECT_EQ(40320, Factorial(8));
}// Step 3. Call RUN_ALL_TESTS() in main().
//
// We do this by linking in src/gtest_main.cc file, which consists of
// a main() function which calls RUN_ALL_TESTS() for us.
//
// This runs all the tests you've defined, prints the result, and
// returns 0 if successful, or 1 otherwise.
//
// Did you notice that we didn't register the tests?  The
// RUN_ALL_TESTS() macro magically knows about all the tests we
// defined.  Isn't this convenient?

Assertions 宏详解

Googletest 断言就是宏。通过断言测试一个函数或者类的行为。
出现错误时,googletest会打印源文件、代码行数和错误信息。
结果只有三种,分别是成功,非致命错误,致命错误,即success, nonfatal failure, fatal failure。非致命错误不会终止当前函数,致命错误会终止测试程序并且退出,也就是说接下来的测试就不会运行了。
一般使用EXPECT_*,这样可以跑完整个测试程序。

宏的格式有两种:

产生效果
ASSERT_*致命错误
EXPECT_*非致命错误

一般来说,EXPECT_*用得更多,因为可显示的错误信息不止一个。除非某条件不成立,程序就无法运行时,就使用ASSERT__*。

使用<<运算符来打印错误信息,示例:

ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length";for (int i = 0; i < x.size(); ++i) {EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i;
}

Basic Assertions

最基础的真假条件判断

Fatal assertionNonfatal assertionVerifies
ASSERT_TRUE(condition);EXPECT_TRUE(condition);condition is true
ASSERT_FALSE(condition);EXPECT_FALSE(condition);condition is false

Binary Comparison

判断两个值

Fatal assertionNonfatal assertionVerifies
ASSERT_EQ(val1, val2);EXPECT_EQ(val1, val2);val1 == val2
ASSERT_NE(val1, val2);EXPECT_NE(val1, val2);val1 != val2
ASSERT_LT(val1, val2);EXPECT_LT(val1, val2);val1 < val2
ASSERT_LE(val1, val2);EXPECT_LE(val1, val2);val1 <= val2
ASSERT_GT(val1, val2);EXPECT_GT(val1, val2);val1 > val2
ASSERT_GE(val1, val2);EXPECT_GE(val1, val2);val1 >= val2

googletest官方文档

其实有一个更简单的方式,使用vcpkg,vcpkg是微软出的C++库管理器,非常好用。基本上流行的开源的库都有。
如何使用vcpkg在我的一篇文章中有,googletest使用vcpkg编译


http://chatgpt.dhexx.cn/article/1GuhcjpK.shtml

相关文章

8051单片机基础4:数据存储类型(data,idata,xdata,code)

文章目录 存储类型使用方法详细说明8051存储器结构dataidatabdatapdataxdatacode 关于作者 参考来源&#xff1a;Keil > Help > uVision Help > Cx51 Compiler User’s Guide > Language Extensions > Memory Types 存储类型 Cx51编译器可以访问8051的所有存储…

安装xdata步骤

1.修改配置文件/etc/security/limits.conf 修改配置文件/etc/security/limits.conf 在该配置文件中添加&#xff1a; vim /etc/security/limits.conf * soft nofile 65535 * hard nofile 65535修改之后重新SSH连接即可&#xff0c;不需要重启主机或服务 2. 禁用禁用SELINUX …

ObjectARX开发笔记(二)——使用Xdata向AutoCAD图形追加自定义数据

1.自定义数据 ObjectARX开发过程中&#xff0c;有时会使用自定义数据&#xff0c;主要包括&#xff1a; 自定义对象——可以让用户按照自己希望的方式封装数据&#xff0c;并向AutoCAD中添加第三方实体类型&#xff0c;这些实体类型用于与AutoCAD自身实体一样的特性&#xff…

matlab xdata ydata,matlab中set(head1,'xdata',x1,'ydata',y1)什么意思?

满意答案 amuese6418 2014.10.25 采纳率&#xff1a;59% 等级&#xff1a;12 已帮助&#xff1a;8722人 这个主要是set的用法 它是功能 设置对象的属性。 用法 set(H,PropertyName,PropertyValue,…) 用属性值PropertyValue设置关于用参量H 标志的对象(一个或多个)的属性名P…

用Proteus仿真C51时用到xdata时的问题

在用做一个程序&#xff0c;但是51的内存资源太过稀有&#xff0c;使用到较大的数组的时候资源很快就耗尽了&#xff0c;可能会遇到以下错误&#xff1a; src\main.c(195): error C249: DATA: SEGMENT TOO LARGE这时需要用到大模式下编译。 然而在proteus下默认xdata是必须加…

xdata,volatile,extern,static,#pragma 关键字

1. xdata xdata&#xff1a;声明的变量位于外部 RAM 地址范围内某一位置。 2. volatile volatile: 的本意是“易变的” &#xff0c;因为访问寄存器要比访问内存单元快的多&#xff0c;所以编译器一般都会作减少存取内存的优化。当要求使用 volatile 声明变量值的时候&#…

51 单片机 data idata xdata 使用

51 单片机 data idata xdata 使用 简介 该篇文章只提使用&#xff0c;先不详细讲解&#xff0c;使用的原因就是自己写的代码 ROM 超了&#xff01;&#xff01;&#xff01;新建工程默认使用 data Keil 配置如下图 测试芯片FlashRAMSTC15W408AS8Kbyte512byte data idata xd…

TMS XData 4.6.0.1 Full Source XE2-DX10.3

TMS XData 4.6.0.1 Full Source XE2-DX10.3 用于多层REST / JSON HTTP / HTTPS应用服务器开发和ORM远程处理的Delphi框架。 TMS XData功能概述 基于REST / JSON架构样式的服务器 由于它是基于REST / JSON的&#xff0c;因此可以轻松地从不同的客户端平台(如.NET&#xff0c;Jav…

10.扩展数据(XData)

愿你出走半生,归来仍是少年&#xff01; 环境&#xff1a;.NET FrameWork4.5、ObjectArx 2016 64bit、Entity Framework 6. CAD中所有数据库对象都包含一个可以供开发人员进行自定义的对象&#xff1a;XDATA,本文主要针对此对象进行探索。 XDATA的使用中需要使用到部分DXFCOD…

【51】单片机中的data、idata、xdata、code等关键字的作用

说明 data&#xff1a;RAM低128字节&#xff0c;响应速度最快&#xff0c;采用直接寻址方式 idata&#xff1a;RAM高128字节&#xff0c;采用间接寻址方式&#xff0c;速度相对较慢 bdata&#xff1a;片内RAM&#xff0c;位寻址方式 xdata&#xff1a;外部扩展RAM&#xff…

Kmeans算法

Kmeans算法 是最简单的聚类算法之一&#xff0c;算法接受参数 k &#xff1b;然后将事先输入的n个数据对象划分为k个聚类以便使得所获得的聚类满足&#xff1a;同一聚类中的对象相似度较高&#xff1b;而不同聚类中的对象相似度较小。聚类相似度是利用各聚类中对象的均值所获得…

图像分割与实战(一)——基于主动轮廓(snake)的图像分割

1.主动轮廓图像分割算法的概述 1.1主要思想 将图像分割问题转化为轮廓进化问题 1.2工作原理 将一条曲线在内外力的共同作用下&#xff0c;使得曲线逐步收敛到目标轮廓 1.3 算法流程 初始化曲线利用内外力更新曲线判断曲线是否收敛&#xff0c;否则返回第2步 1.4 需要解决…

实时实例分割算法Deep Snake,速度32.3fps|CVPR 2020

论文提出基于轮廓的实例分割方法Deep snake&#xff0c;轮廓调整是个很不错的方向&#xff0c;引入循环卷积&#xff0c;不仅提升了性能还减少了计算量&#xff0c;保持了实时性&#xff0c;但是Deep snake的大体结构不够优雅&#xff0c;应该还有一些工作可以补&#xff0c;推…

数字图像处理笔记——Snakes算法、活动轮廓以及水平集(Snakes, active contours, and level sets)

Snakes算法 上一讲我们讲的图像分割算法主要是基于像素的&#xff0c;这一讲主要是基于曲线的。我们希望能得到一个能够包围住图像轮廓的平滑的曲线&#xff0c;snakes算法就是一个很有用的算法。首先我们将曲线的坐标x、y同一用参数s表示&#xff0c;s范围从0-1代表从起点绕曲…

Deep Snake for Real-Time Instance Segmentation:基于Deep Snake的实例实时分割

本文针对自己所看的 Deep Snake 做一个总结和存档&#xff0c;也方便其他同学学习 —— 阿波&#xff0c;2020.4.23 论文原文&#xff1a;Deep Snake for Real-Time Instance Segmentation 代码地址&#xff1a; snake 本篇文章工作有以下贡献&#xff1a; •提出了一种基于…

KMeans算法流程

一、什么是簇&#xff1f; 我们知道聚类就是让机器把数据集中的样本按照特征的性质分组&#xff0c;直观上来看&#xff0c;簇是一组一组聚集在一起的数据&#xff0c;在 一个簇 中的数据就认为是 同一类 &#xff0c;簇就是聚类的结果表现。实际上簇并没有明确的定义&#xf…

HZNUCTF MISC Snake题解——python程序逆向,hashcat爆破sha256

目录 一.Dump得到pyc文件 二.pyc反编译得到py源码 三.分析程序逻辑 四.hashcat爆破 题目附件链接&#xff1a;https://pan.baidu.com/s/1CcS8BPGx8fKnsJgRvEi0bA?pwdt2yj 提取码&#xff1a;t2yj 一.Dump得到pyc文件 使用命令:python pyinstxtractor.py snake.exe 二.p…

DeepSnake实现实例分割

文章目录 简介&#xff1a;环境配置Demo实现全文总结参考文献 简介&#xff1a; DeepSnake(Deep Snake for Real-Time Instance Segmentation)作为CVPR 2020 oral论文&#xff0c;在实例分割任务上取得了实时分割效果的同时&#xff0c;还保持着非常不错的性能&#xff0c;该论…

【主动轮廓模型(二)】《GVF Snake》算法原理与OpenCV实现

文章目录 1 问题引入1.1 传统Snake模型的缺陷1.2 亥姆霍兹定理&#xff08;Helmholtz theorem&#xff09; 2 GVF Snake2.1 边缘图&#xff08;Edge Map&#xff09;2.2 梯度矢量流&#xff08;Gradient Vector Flow&#xff0c;GVF&#xff09;2.3 数值求解方法 3 OpenCV实现 …