内容简介:未完,待续...
未完,待续...
一、基本概念
容易混淆的概念
Test
Test(测试)
Test Case
Test Case(测试用例)
Test Suite
Test Suite(测试套件)
断言
ASSERT_*
失败时会生成致命的故障,并中止当前的功能。
EXCEPT_*
生成非致命故障,不会中止当前功能。通常是首选。
基本断言
在任何一种情况下,断言失败意味着其包含的测试失败。
| 致命的断言 | 非致命断言 | 验证 |
|---|---|---|
| ASSERT_TRUE(condition); | EXPECT_TRUE(condition); | condition is true |
| ASSERT_FALSE(condition); | EXPECT_TRUE(condition); | condition is false |
二、搭建使用环境
1. 下载源码
git clone https://github.com/google/googletest.git
2. 编译源码并安装库
# build 与 下载的 googletest 位于同一层级 mkdir build && cd build # 生成本地 Makefile 文件 cmake ../googletest/ # 编译源码 make # 安装头文件和库文件 sudo make install # 安装文件目录说明(Ubnutu) 头文件:/usr/local/include 库文件:/usr/local/lib
三、建立测试程序
1. 编写测试代码
// main.c
//
// 包含 googletest 头文件
#include <gtest/gtest.h>
// 待测函数
int fun(int a) {
return a + 1;
}
// 单元测试
TEST(FunTest, HandlesZeroInput) {
EXPECT_EQ(1, fun(0));
}
// 测试入口
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
2. 编译代码
# 编译方式一 g++ hello.cpp /usr/local/lib/libgtest.a -lpthread -std=c++11 -o hello # 编译方式二 g++ -std=c++11 hello.cpp -L/usr/local/lib -lgtest -lpthread -o hello
3. 运行程序
$ ./hello [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from FunTest [ RUN ] FunTest.HandlesZeroInput [ OK ] FunTest.HandlesZeroInput (0 ms) [----------] 1 test from FunTest (0 ms total) [----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 1 test.
相关资料
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Designing for Emotion
Aarron Walter / Happy Cog / 2011-10-18 / USD 18.00
Make your users fall in love with your site via the precepts packed into this brief, charming book by MailChimp user experience design lead Aarron Walter. From classic psychology to case studies, high......一起来看看 《Designing for Emotion》 这本书的介绍吧!