Mac下GTest的基本使用

gtest全称Google C++ Testing Framework,它是从谷歌内部诞生并受到业界追捧的一个非常优秀的测试框架,支持如自动发现测试、自定义断言、死亡测试、自动报告等诸多功能。

本文记录Mac下安装配置gtest以及它的基本应用

一、安装

博主尝试用brew install 遇到了问题,于是直接源码安装

编译安装

 git clone https://github.com/google/googletest
cd googletest
mkdir build && cd build
cmake ..
make
make install

测试代码

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
set(CMAKE_CXX_STANDARD 11) project(demo) find_package(GTEST REQUIRED) add_executable(${PROJECT_NAME} main.cpp) target_link_libraries(${PROJECT_NAME} ${GTEST_LIBRARIES})

main.cpp

#include <iostream>
#include <gtest/gtest.h> int add(int a, int b) {
return a + b;
} int sub(int a, int b) {
return a - b;
} // case1
TEST(test, c1) {
EXPECT_EQ(3, add(1, 2));
EXPECT_EQ(12, add(2, 6));
} // case2
TEST(test, c2) {
EXPECT_EQ(-1, sub(1, 2));
} GTEST_API_ int main(int argc, char ** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

编译运行

cmake .
make
./demo
# 输出
➜ build ./demo
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from test
[ RUN ] test.c1
/Users/fanghao/Desktop/test/main.cpp:15: Failure
Expected equality of these values:
12
add(2, 6)
Which is: 8
[ FAILED ] test.c1 (0 ms)
[ RUN ] test.c2
[ OK ] test.c2 (0 ms)
[----------] 2 tests from test (0 ms total) [----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (0 ms total)
[ PASSED ] 1 test.
[ FAILED ] 1 test, listed below:
[ FAILED ] test.c1 1 FAILED TEST

二、基本概念

  • 断言:即assertion,检查条件是否为真,一个断言的结果有

    • sucess
    • nonfatal failure(非致命失败)
    • fatal failure(致命失败)

    这里值得注意的是后两个,nonfatal failure会指出错误,然后继续运行,由EXPECT_XX产生,fatal failure会指出错误但是终止当前测试函数,由ASSERT_XX产生,一般推荐用前者,这样一次可以查出多个问题

  • 测试样例:test case表示要测试的对象,一般TEST会有两个参数,分别是测试样例,以及测试的子case,如下面这个测试阶乘的样例,HandlesZeroInput和HandlesPositiveInput都属于FactorialTest这个测试样例

    // Tests factorial of 0
    TEST(FactorialTest, HandlesZeroInput) {
    EXPECT_EQ(1, Factorial(0));
    } // Tests factorial of positive numbers.
    TEST(FactorialTest, HandlesPositiveInput) {
    EXPECT_EQ(1, Factorial(1));
    EXPECT_EQ(2, Factorial(2));
    EXPECT_EQ(6, Factorial(3));
    EXPECT_EQ(40320, Factorial(8));
    }
  • 常用断言

    Fatal assertion Nonfatal assertion Verifies
    ASSERT_TRUE(condition); EXPECT_TRUE(condition); condition is true
    ASSERT_FALSE(condition); EXPECT_FALSE(condition); condition is false
    ASSERT_EQ(expected, actual); EXPECT_EQ(expected, actual); expected == actual
    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
    ASSERT_STREQ(expected_str, actual_str); EXPECT_STREQ(expected_str, actual_str); the two C strings have the same content
    ASSERT_STRNE(str1, str2); EXPECT_STRNE(str1,str2); the two C strings have different content
    ASSERT_STRCASEEQ(expected_str, actual_str); EXPECT_STRCASEEQ(expected_str, actual_str); the two C strings have the same content,ignore case
    ASSERT_STRCASENE(str1, str2); EXPECT_STRCASENE(str1,str2); the two C strings have different content,ignore case

三、 Test Fixture

Test Fixture是对基本test case的封装,如果多个test需要操作类似数据,可以把他们封装在一个类里面,然后共享一些配置。

编写Fixure步骤

  1. 建立一个类,并继承::testing::Test,并且使用protected或public限制符,以便其子类可以访问到共享的数据
  2. 在这个类中,声明你想复用的对象。
  3. 写一个SetUp函数来准备所需对象
  4. 写一个TearDown函数来释放资源
  5. 其他用法和基本的test case一样,只是用TEST_F()宏代替TEST()宏,每次调用一个TEST_F都会重新运行一次SetUp和TearDown,但但成员对象可以共享

看一个示例应该更能说明问题

#include <iostream>
#include <gtest/gtest.h> class Foo
{
public:
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
}; class FooTest : public ::testing::Test
{
protected:
Foo foo; // shared object
// SetUp && TearDown will be called for every Test Case
virtual void SetUp()
{
std::cout << "Code here will be called immediately after the constructor" << std::endl;
}
virtual void TearDown()
{
std::cout << "Code here will be called immediately after each test" << std::endl;
}
}; // Test Case 1
TEST_F(FooTest, TestMethodAdd)
{
EXPECT_EQ(3, foo.add(1, 2));
} // Test Case 2
TEST_F(FooTest, TestMethodSub)
{
EXPECT_EQ(-1, foo.sub(1, 2));
} int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
} # 输出
➜ build ./demo
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from FooTest
[ RUN ] FooTest.TestMethodAdd
Code here will be called immediately after the constructor
Code here will be called immediately after each test
[ OK ] FooTest.TestMethodAdd (0 ms)
[ RUN ] FooTest.TestMethodSub
Code here will be called immediately after the constructor
Code here will be called immediately after each test
[ OK ] FooTest.TestMethodSub (0 ms)
[----------] 2 tests from FooTest (0 ms total) [----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (1 ms total)
[ PASSED ] 2 tests.

四、小结

感觉gtest还是挺优雅的,适合TDD模式,把测试和开发完全解耦。

最新文章

  1. TypeScript &amp; JavaScript
  2. Double 数据保留两位小数二:直接截取小数后面两位,不进行四舍五入
  3. 将excel2003文档文件转换为excel2007格式
  4. 使用node.js制作简易爬虫
  5. Linux设备文件自动生成
  6. Swift编程语言学习10—— 枚举属性监视器
  7. Linux下一个OTL 采用long long类型数据库支持BIGINT
  8. 【.NET】电话号码打星号(隐藏部分)
  9. iOS开发——闪光灯
  10. App 组件化/模块化之路——Android 框架组件(Android Architecture Components)使用指南
  11. python的multiprocessing模块进程创建、资源回收-Process,Pool
  12. python爬取猫眼电影top100
  13. 机器学习入门14 - 神经网络简介 (Introduction to Neural Networks)
  14. MySQL之You can't specify target table for update in FROM clause解决办法
  15. Android 开发 深入理解Handler、Looper、Messagequeue 转载
  16. java 之程序中的http请求
  17. 前端 --- 7 Bootstrop框架
  18. Unreal Open Day游记
  19. gulp + angular + requirejs 简单学习
  20. NP难问题

热门文章

  1. Go语言系列(三)- 基础函数和流程控制
  2. ArrayList的实现及原理
  3. JSON三种数据解析方法(转)
  4. 基于alpine制作php镜像
  5. 类型和原生函数及类型转换(二:终结js类型判断)
  6. [再寄小读者之数学篇](2014-10-27 Frobenius 范数是酉不变范数)
  7. [物理学与PDEs]第1章第9节 Darwin 模型 9.1 拟静电模型及其修正形式
  8. 2.5 elif
  9. 字符串常量池和String.intern()方法在jdk1.6、1.7、1.8中的变化
  10. JAVA进阶11