如果你还没有搭建gtest框架,可以参考我之前的博客:http://www.cnblogs.com/jycboy/p/6001153.html。。

1.The first sample: sample1

你把github上的项目导来之后,github地址:https://github.com/google/googletest,在目录:..(你的目录)\googletest-master\googletest\samples是你的samples文件夹。

在VS中创建项目:GtestSamples

把对应的代码加入到这里边:sample1.h、sample1.cc、sample1_unittest.cc.

在sample1.cc中是你要测试的函数:

// 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;
} // Returns true iff n is a prime number.
bool IsPrime(int n) {
// Trivial case 1: small numbers
if (n <= 1) return false; // Trivial case 2: even numbers
if (n % 2 == 0) return n == 2; // Now, we have that n is odd and n >= 3.
// Try to divide n by every odd number i, starting from 3
for (int i = 3; ; i += 2) {
// We only have to try i up to the squre root of n
if (i > n / i) break; // Now, we have i <= n/i < n.
// If n is divisible by i, n is not prime.
if (n % i == 0) return false;
}
// n has no integer factor in the range (1, n), and thus is prime.
return true;
}

在sample1_unittest.cc中,是你编写的测试:

有两个测试用例:Factorial、IsPrime;每个测试用例对应三个test。

extern int Factorial(int n);
extern bool IsPrime(int n);
// 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(2, Factorial(-1)); //如果是ASSERT_EQ,后边的EXPECT_GT将不在执行;如果是EXPECT_EQ,后边的测试EXPECT_GT继续执行
EXPECT_GT(Factorial(-10), 0);
ASSERT_EQ(3, Factorial(-1)); // <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));
} // Tests IsPrime() // Tests negative input.
TEST(IsPrimeTest, Negative) {
// This test belongs to the IsPrimeTest test case. EXPECT_FALSE(IsPrime(-1));
EXPECT_FALSE(IsPrime(-2));
EXPECT_FALSE(IsPrime(INT_MIN));
} // Tests some trivial cases.
TEST(IsPrimeTest, Trivial) {
EXPECT_FALSE(IsPrime(0));
EXPECT_FALSE(IsPrime(1));
EXPECT_TRUE(IsPrime(2));
EXPECT_TRUE(IsPrime(3));
} // Tests positive input.
TEST(IsPrimeTest, Positive) {
EXPECT_FALSE(IsPrime(4));
EXPECT_TRUE(IsPrime(5));
EXPECT_FALSE(IsPrime(6));
EXPECT_TRUE(IsPrime(23));
} // 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?

  

2.编写单元测试的步骤

Step 1.包括必要的头文件,以便声明测试逻辑需要的东西。不要忘了 gtest.h

#include <limits.h>
#include "sample1.h"
#include <gtest/gtest.h>

 Step 2. 使用TEST宏来定义测试。

TEST有两个参数:测试用例名称和测试名称。
使用宏后,应该在一对大括号之间定义测试逻辑。 您可以使用一堆宏来指示测试的成功或失败。 EXPECT_TRUE和EXPECT_EQ是此类宏的示例。 有关完整列表,请参阅gtest.h。

 技术细节:

在Google Test中,测试分为多个测试用例。你应该将逻辑相关的测试放入同一个测试用例。
测试用例名和测试名都应该是有效的C ++标识符。 并且你不应该在名称中使用下划线(_)。

Google测试保证您定义的每个测试只运行一次,但不能保证测试执行的顺序。 因此,您应该以这样一种方式编写测试,使得它们的结果不依赖于它们的顺序

TEST(FactorialTest, Negative) {
// This test is named "Negative", and belongs to the "FactorialTest"
// test case.
EXPECT_EQ(1, Factorial(-5));
EXPECT_EQ(2, Factorial(-1)); //如果是ASSERT_EQ,后边的EXPECT_GT将不在执行;如果是EXPECT_EQ,后边的测试EXPECT_GT继续执行
EXPECT_GT(Factorial(-10), 0);
EXPECT_TRUE(2 == Factorial(-7));//这个错误失败信息打印的是true或false,不会打印值
ASSERT_EQ(3, Factorial(-1));
}

技术细节:

上面的EXPECT_EQ(1, Factorial(-1))和EXPECT_TRUE(1==Factorial(-1))的作用是一样的,但是当失败时,EXPECT_EQ会打印期望值和实际值,而EXPECT_TRUE是会打印true、false。所以优先选用EXPECT_EQ。

Step 3. Call RUN_ALL_TESTS() in main().

int main(int argc, char **argv) {
//printf("Running main() from gtest_main.cc\n");
testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}

   **RUN_ALL_TESTS() 会自动调用所有的测试。

之前的两篇博客:

VS2015搭建GoogleTest框架--配置第一个项目

Google C++单元测试框架---Gtest框架简介(译文)

之后会陆续更新,一直到GMock,但愿我不会太懒,。。。。

Google C++单元测试框架GoogleTest---GTest的Sample1和编写单元测试的步骤的更多相关文章

  1. Google C++单元测试框架GoogleTest(总)

    之前一个月都在学习googletest框架,对googletest的文档都翻译了一遍,也都发在了之前的博客里,另外其实还有一部分的文档我没有发,就是GMock的CookBook部分:https://g ...

  2. c++单元测试框架googletest

    一.概述 Googletest是一个用来写C++单元测试的框架,它是跨平台的,可应用在windows.linux.Mac等OS平台上: 代码框架: [root@docker googletest-re ...

  3. Javascript单元测试框架比较Qunit VS Jasmine

    Javascript单元测试框架比较Qunit VS Jasmine 工欲行其事必先利其器,好的单元测试框架是TDD成功的一半.Javascript优秀的测试框架很多, 包括Jasmine,Qunit ...

  4. Keil中搭建自动化单元测试框架Unity

    前言: 虽然一些C++的自动化单元测试框架也能用来C语言单元测试,但那样我们编写C语言程序时需要符合C++的标准,这样有一些C的特性是无法使用的,限制C的特性使用不太好,于是找了一个全部用C实现的自动 ...

  5. 使用gtest对DLL工程进行单元测试的实践

    前言 关于单元测试的重要性.gtest的优缺点等就不说了.之前项目是没有做单元测试的,在VS的解决方案中,只有一个可执行的工程,其他的工程都是以DLL库的形式提供.本文只针对使用VS对DLL库进行单元 ...

  6. 单元测试框架之unittest(一)

    一.单元测试的含义 unittest单元测试框架的设计灵感来源于Junit(Java语言的单元测试框架),它与其他语言的单元测试框架风格相类似,支持自动化测试.为测试共享setUp和shutDown. ...

  7. 玩转Google开源C++单元测试框架Google Test系列(gtest)(转)

    转自:http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 前段时间学习和了解了下Google的开源C++单元测试框架Googl ...

  8. 玩转Google开源C++单元测试框架Google Test系列(gtest)(总)

    原文地址:http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 前段时间学习和了解了下Google的开源C++单元测试框架Goo ...

  9. [转]玩转Google开源C++单元测试框架Google Test系列(gtest)(总)

    文章转载自CoderZh的技术博客 地址:https://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 前段时间学习和了解了下Goog ...

随机推荐

  1. 移动端web开发

    meta基础知识点: 页面自动调整到设备宽度,并禁止用户缩放. <meta name="viewport" content="width=device-width, ...

  2. Android重写菜单增加系统自带返回键

    条件:当前项目导入了ActionBarSherlock这个jar包,这个jar包的作用为了程序的兼容性,考虑低版本的问题. 学习ActionBarSherlock参考博客链接:http://blog. ...

  3. C# 对象锁——Monitor

    Monitor里边有一些static方法,可以用于在对象上获取同步锁,来进行一些进程同步控制操作 用法及注意点如下: using System; using System.Collections.Ge ...

  4. geotrellis使用(二)geotrellis-chatta-demo以及geotrellis框架数据读取方式初探

    在上篇博客(geotrellis使用初探)中简单介绍了geotrellis-chatta-demo的大致工作流程,但是有一个重要的问题就是此demo如何调取数据进行瓦片切割分析处理等并未说明,经过几天 ...

  5. php+phpStorm+xdebug配置方法

    1.下载xdebug文件 http://xdebug.org/wizard.php 将phpinfo()的源代码复制到文本框中,xdebug会提示如何配置和下载哪个版本的xdebug. 全部下载地址: ...

  6. C# 设置word文档页面大小

    我们知道,在MS word中,默认的页面大小是letter(8.5’’x11’’),除此之外,word还提供了其他一些预定义的页面大小,如Legal (5.4’’x14’’),A3 (11.69’’x ...

  7. Oracle Tuning 基础概述01 - Oracle 常见等待事件

    对Oracle数据库整体性能的优化,首先要关注的是在有性能问题时数据库排名前几位等待事件是哪些.Oracle等待事件众多,随着版本的升级,数量还在不断增加,可以通过v$event_name查到当前数据 ...

  8. AppCan学习笔记--数据存储及listview简单应用

    AppCan AppCan开发平台简介 AppCan是Hybrid App开发框架即混合开发框架,有官方提供底层功能使用API HTML5和JavaScript只是作为一种解析语言,真正调用的都是Na ...

  9. Android测试提升效率批处理脚本

    前言: APP测试过程中,经常需要用的一些命令,如adb,每次敲命令,虽可以加深印象,但个人认为那即繁琐又浪费时间.本文贴出一些我使用的批处理,以及一点点小小技巧. 目录 1.[查看APK文件信息.b ...

  10. 实现terminal代理

    问题 作为一名Linuxer,熟练使用终端是一项必备技能,但终端中有时下载安装功能速度很慢,令人崩溃.我自然而然想到了可否加个代理,提高速度.我之前一直用shadowsocks,浏览器使用switch ...