Google Test简述
Google Test简述
Google Test是谷歌推出的C++测试框架,谷歌出品,实力肯定是有保障的。官方文档在文章末尾。
如果可以还是建议看英文的源文档
特点
- 跨平台
- 不止单元测试
- 稳定并且可重复
- 提供足够的失败信息
- 快速
- 线程安全
支持的平台,编译器和构建系统
平台
- 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 -S . -B build
直接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,如果没有下载需要下载,cmake是现代许多C/C++项目的构建器,掌握了是很有好处的,cmake相当于对MakeFile的封装,其实底层还是调用的gcc/g++等编译器。
使用cmake编译:
git clone https://github.com/google/googletest.git -b release-1.10.0 #拉取源代码
cd googletest # Main directory of the cloned repository.
mkdir build # Create a directory to hold the build output.
cd build
cmake .. # Generate native build scripts for GoogleTest.
如果接着输入如下代码,就可以直接在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 assertion | Nonfatal assertion | Verifies |
---|---|---|
ASSERT_TRUE(condition);
|
EXPECT_TRUE(condition);
|
condition is true
|
ASSERT_FALSE(condition);
|
EXPECT_FALSE(condition);
|
condition is false
|
Binary Comparison
判断两个值
Fatal assertion | Nonfatal assertion | Verifies |
---|---|---|
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编译
Google Test简述相关推荐
- 强化学习 - Deep RL开源项目总结
https://zhuanlan.zhihu.com/p/24392239 一. Lua 语言的程序包(运用框架:Torch 7): 1. 相关论文:Human-level control throu ...
- 转:解决“连接被重置”
来自:http://hi.baidu.com/kangakang203/item/721eca179da91ef9746a841d 让"连接被重置"去见鬼--稳定地使用Google ...
- Google浏览器搜索简述
说太专业了不懂.先简单的做一下比喻吧.我们的网页们在网上就像现实世界的一个个地址里的人家,但是要数字化的访问,可以把地址转换为编号,DNS就是把地址转换为统一的编号的装置.(至于Domain Name ...
- 5 分钟入门 Google 最强NLP模型:BERT
BERT (Bidirectional Encoder Representations from Transformers) 10月11日,Google AI Language 发布了论文 BERT: ...
- 简述移动端IM开发的那些坑:架构设计、通信协议和客户端
1.前言 有过移动端开发经历的开发者都深有体会:移动端IM的开发,与传统PC端IM有很大的不同,尤其无线网络的不可靠性.移动端硬件设备资源的有限性等问题,导致一个完整的移动端IM架构设计和实现都充满着 ...
- Google Maps和GIS开发资源收集
Google Maps JQuery Maps google map是怎样工作的 Google Map API基本概念 Google Maps API编程资源大全 google map限制地图缩放级别 ...
- Google MapReduce有啥巧妙优化?
搞架构的人,Google的架构论文是必看的,但好像大家都不愿意去啃英文论文.故把自己的读书笔记,加入自己的思考,分享给大家. <MapReduce到底解决什么问题?>做了简介,这是第二篇, ...
- Google Gears 指南
Gears概述 Google Gears 是一个开源的浏览器插件,它支持用户浏览器能够使用开发者创建的基于 Gears 的离线应用.它提出了一种离线应用的思想,它的三大核心模块将帮助开发者更好地运用这 ...
- 一图简述大数据技术生态圈
下面是一张生态图,主要的组件都是为了方便大家从底层的MapReduce模型中脱离出来,用高层语言来做分布式计算,下文将分别为你作简述. 1.HBase 是一个高可靠性.高性能.面向列.可伸缩的分布式存 ...
最新文章
- android系统休眠发广播,Android - BroadcastReceiver
- SCAPY pcap文件数据分析 python3
- python关键字是什么颜色,python – Matplotlib:如果使用关键字sym,则使用Boxplot异常值颜色更改...
- GDCM:从ELSCINT1读取Wave Information标签的测试程序
- 意犹未尽 —— GPM 的状态流转(十)
- mysql 格林时间转换_格林时间转换成正常时间
- 大专学计算机应用难吗,上了两年技校,专业是计算机应用,什么也没学到。现在想在去上个大专。学什么专业好呢。?...
- win10鼠标灵敏度怎么调_和平精英灵敏度怎么设置才最合适 调最适合自己用的
- 轻薄于型 强悍于内 拯救者9000X 2021硬核发布
- 正整数变成英文输出c语言,C语言,输入一个正整数,然后从个位开始一次输出每一位数字对应的英文字母...
- CAP、BASE理论
- Pale Moon 15.3 - Firefox“苍月”优化版发布
- CSS属性vertical-align详解(CSS之五)
- R语言文本挖掘展示:画词云图
- ERROR 1010 (HY000): Error dropping database (can‘t rmdir ‘.\qpweb‘, errno: 41) 删库失败问题的解决
- 连续复利怎么用计算机算,请问银行的连续复利计算公式
- Linux CentOS删除或重命名文件夹和文件的办法
- YOLO—V1细节分析
- C游戏编程入门第一讲心得
- JS的3种加密方式:base64编码、md5加密及sha加密
热门文章
- UHD630核显驱动方法及驱动后闪屏严重问题解决记录
- 视频:Damien Katz畅谈CouchDB
- Python初学者(零基础学习Python、Python入门)书籍、视频、资料、社区推荐
- 数据结构(C语言)——2栈:FILO顺序判定
- 运行gulp build时报错:error Expected linebreaks to be 'LF' but found 'CRLF' linebreak-style
- JavaWeb中访问路径问题
- 我用Python爬取了难下载的电子教材(内附代码)
- 中国劳动力动态调查数据(CLDS)
- android 开发中不可不知的技巧
- 【feature_column处理】- crossed_column交叉列