Google TestExtending Google Test by Handling Test Events

Google测试提供了一个事件侦听器API,让您接收有关测试程序进度和测试失败的通知。 可以监听的事件包括测试程序的开始和结束,测试用例或测试方法等。 您可以使用此API来扩充或替换标准控制台输出,替换XML输出,或提供完全不同的输出形式,例如GUI或数据库。 例如,您还可以使用测试事件作为检查点来实现资源泄漏检查器。

一、定义事件侦听器

要定义一个事件监听器,你需要继承testing :: TestEventListener或testing :: EmptyTestEventListener。前者是一个(抽象)接口,其中每个纯虚方法
可以重写以处理测试事件(例如,当测试开始时,将调用OnTestStart()方法。)。后者提供了接​​口中所有方法的空实现,使得子类只需要覆盖它关心的方法。

当一个事件触发时,它的上下文作为参数传递给处理函数。使用以下参数类型

  • UnitTest反映整个测试程序的状态,
  • TestCase包含关于一个测试用例的信息,它可以包含一个或多个测试,
  • TestInfo包含测试的状态,和
  • TestPartResult表示测试断言的结果。

事件处理函数可以检查它接收的参数,以找到关于事件和测试程序状态的有趣信息。这里有一个例子:

class MinimalistPrinter : public ::testing::EmptyTestEventListener {
// Called before a test starts.
virtual void OnTestStart(const ::testing::TestInfo& test_info) {
printf("*** Test %s.%s starting.\n",
test_info.test_case_name(), test_info.name());
} // Called after a failed assertion or a SUCCEED() invocation.
virtual void OnTestPartResult(
const ::testing::TestPartResult& test_part_result) {
printf("%s in %s:%d\n%s\n",
test_part_result.failed() ? "*** Failure" : "Success",
test_part_result.file_name(),
test_part_result.line_number(),
test_part_result.summary());
} // Called after a test ends.
virtual void OnTestEnd(const ::testing::TestInfo& test_info) {
printf("*** Test %s.%s ending.\n",
test_info.test_case_name(), test_info.name());
}
};

二、使用事件监听器

要使用您定义的事件侦听器,请将其实例添加到Google Test事件侦听器列表中(由TestEventListeners类表示)

  • note the "s" at the end of the name) in your main() function, before calling RUN_ALL_TESTS():
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
// Gets hold of the event listener list.
::testing::TestEventListeners& listeners =
::testing::UnitTest::GetInstance()->listeners();
// Adds a listener to the end. Google Test takes the ownership.
listeners.Append(new MinimalistPrinter);
return RUN_ALL_TESTS();
}

  只有一个问题:默认测试结果打印机仍然有效,因此其输出将与您的简约打印机的输出混合。 要禁止默认打印机,只需从事件侦听器列表中释放它并删除它。 您可以添加一行:

...
delete listeners.Release(listeners.default_result_printer());
listeners.Append(new MinimalistPrinter);
return RUN_ALL_TESTS();

现在,坐下来享受与你的测试完全不同的输出。 有关更多详细信息,您可以阅读此示例 sample(sample9_unittest)。

您可以向列表附加多个侦听器。 当On * Start()或OnTestPartResult()事件触发时,监听器将按它们在列表中显示的顺序接收它(因为新的监听器添加到列表的末尾,默认文本打印机和默认XML生成器 将第一时间接收事件)。 An On * End()事件将由侦听器以相反的顺序接收。 这允许稍后添加的侦听器的输出由之前添加的侦听器的输出构成。

三、Generating Failures in Listeners

在处理事件时,可以使用故障提升宏(EXPECT _ *(),ASSERT _ *(),FAIL()等)。 有一些限制:

  • 你不能在OnTestPartResult()中产生任何失败(否则会导致OnTestPartResult()被递归调用)。
  • 处理OnTestPartResult()的侦听器不允许生成任何失败。

当您向侦听器列表添加侦听器时,应该在可能生成失败的侦听器之前放置处理OnTestPartResult()的侦听器。 这确保由后者产生的故障归因于前者的正确测试。

我们在这里有一个失败监听器的示例here(sample10_unittest)。

四、TestEventListener接口

// The interface for tracing execution of tests. The methods are organized in
// the order the corresponding events are fired.
class TestEventListener {
public:
virtual ~TestEventListener() {} // Fired before any test activity starts.
virtual void OnTestProgramStart(const UnitTest& unit_test) = 0; // Fired before each iteration of tests starts. There may be more than
// one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration
// index, starting from 0.
virtual void OnTestIterationStart(const UnitTest& unit_test,
int iteration) = 0; // Fired before environment set-up for each iteration of tests starts.
virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0; // Fired after environment set-up for each iteration of tests ends.
virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0; // Fired before the test case starts.
virtual void OnTestCaseStart(const TestCase& test_case) = 0; // Fired before the test starts.
virtual void OnTestStart(const TestInfo& test_info) = 0; // Fired after a failed assertion or a SUCCEED() invocation.
virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0; // Fired after the test ends.
virtual void OnTestEnd(const TestInfo& test_info) = 0; // Fired after the test case ends.
virtual void OnTestCaseEnd(const TestCase& test_case) = 0; // Fired before environment tear-down for each iteration of tests starts.
virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0; // Fired after environment tear-down for each iteration of tests ends.
virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0; // Fired after each iteration of tests finishes.
virtual void OnTestIterationEnd(const UnitTest& unit_test,
int iteration) = 0; // Fired after all test activities have ended.
virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0;
}; // The convenience class for users who need to override just one or two
// methods and are not concerned that a possible change to a signature of
// the methods they override will not be caught during the build. For
// comments about each method please see the definition of TestEventListener
// above.一个空实现
class EmptyTestEventListener : public TestEventListener {
public:
virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {}
virtual void OnTestIterationStart(const UnitTest& /*unit_test*/,
int /*iteration*/) {}
virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {}
virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {}
virtual void OnTestCaseStart(const TestCase& /*test_case*/) {}
virtual void OnTestStart(const TestInfo& /*test_info*/) {}
virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {}
virtual void OnTestEnd(const TestInfo& /*test_info*/) {}
virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {}
virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {}
virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {}
virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/,
int /*iteration*/) {}
virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {}
};

五、官方例子

就是samples文件夹下的sample10_unittest.cpp

// This sample shows how to use Google Test listener API to implement
// a primitive leak checker. #include "stdafx.h" using ::testing::EmptyTestEventListener;
using ::testing::InitGoogleTest;
using ::testing::Test;
using ::testing::TestCase;
using ::testing::TestEventListeners;
using ::testing::TestInfo;
using ::testing::TestPartResult;
using ::testing::UnitTest; namespace { // We will track memory used by this class.
class Water {
public:
// Normal Water declarations go here. // operator new and operator delete help us control water allocation.
void* operator new(size_t allocation_size) {
allocated_++;
return malloc(allocation_size);
} void operator delete(void* block, size_t /* allocation_size */) {
allocated_--;
free(block);
} static int allocated() { return allocated_; } private:
static int allocated_;
}; int Water::allocated_ = 0; // This event listener monitors how many Water objects are created and
// destroyed by each test, and reports a failure if a test leaks some Water
// objects. It does this by comparing the number of live Water objects at
// the beginning of a test and at the end of a test.
class LeakChecker : public EmptyTestEventListener { private:
// Called before a test starts.
virtual void OnTestStart(const TestInfo& /* test_info */) {
initially_allocated_ = Water::allocated();
} // Called after a test ends.
virtual void OnTestEnd(const TestInfo& /* test_info */) {
int difference = Water::allocated() - initially_allocated_; // You can generate a failure in any event handler except
// OnTestPartResult. Just use an appropriate Google Test assertion to do
// it.
EXPECT_LE(difference, 0) << "Leaked " << difference << " unit(s) of Water!";
} int initially_allocated_; }; TEST(ListenersTest, DoesNotLeak) {
Water* water = new Water;
delete water;
} // This should fail when the --check_for_leaks command line flag is
// specified.
TEST(ListenersTest, LeaksWater) {
Water* water = new Water;
EXPECT_TRUE(water != NULL);
} } // namespace int main(int argc, char **argv) {
InitGoogleTest(&argc, argv); bool check_for_leaks = false;
if (argc > 1 && strcmp(argv[1], "--check_for_leaks") == 0)
check_for_leaks = true;
else
printf("%s\n", "Run this program with --check_for_leaks to enable "
"custom leak checking in the tests."); // If we are given the --check_for_leaks command line flag, installs the
// leak checker.
if (check_for_leaks) {
TestEventListeners& listeners = UnitTest::GetInstance()->listeners(); // Adds the leak checker to the end of the test event listener list,
// after the default text output printer and the default XML report
// generator.
//
// The order is important - it ensures that failures generated in the
// leak checker's OnTestEnd() method are processed by the text and XML
// printers *before* their OnTestEnd() methods are called, such that
// they are attributed to the right test. Remember that a listener
// receives an OnXyzStart event *after* listeners preceding it in the
// list received that event, and receives an OnXyzEnd event *before*
// listeners preceding it.
//
// We don't need to worry about deleting the new listener later, as
// Google Test will do it.
listeners.Append(new LeakChecker);
}
return RUN_ALL_TESTS();
}

  

Google C++单元测试框架GoogleTest---Extending Google Test by Handling Test Events的更多相关文章

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

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

  2. Google C++单元测试框架GoogleTest---GMock的CheatSheet文档

    CheatSheet文档中包含了GMock所有常用的东西,看了这个基本上就可以用它了,本文接上篇博文:Google C++单元测试框架GoogleTest---Google Mock简介--概念及基础 ...

  3. Google C++单元测试框架GoogleTest---GTest的Sample1和编写单元测试的步骤

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

  4. Google C++单元测试框架GoogleTest---AdvancedGuide(译文)下

    因为AdvancedGuide文档太长,分上下两部分,本文档接googletest--AdvancedGuide(译文)上:Google C++单元测试框架GoogleTest---AdvancedG ...

  5. Google C++单元测试框架

    一.概述 Google C++单元测试框架(简称Gtest),可在多个平台上使用(包括Linux, Mac OS X, Windows, Cygwin和Symbian),它提供了丰富的断言.致命和非致 ...

  6. Google C++单元测试框架GoogleTest---Google Mock简介--概念及基础语法

    就在昨天终于做了gtest的分享,我的预研工作终于结束了,感觉离我辞职的日子不远了,毕竟是专注java二百年啊,要告别实习啦.. 这篇是GoogleMock的简介文档,会在后边附带一个自己的例子. 一 ...

  7. Google C++单元测试框架GoogleTest---AdvancedGuide(译文)上

    本文是gtest高级测试指南的译文,由于文章太长,分上下两部分. 一.简介 本文档将向您展示更多的断言,以及如何构造复杂的失败消息,传播致命的故障,重用和加速您的测试夹具,并在您的测试使用各种标志. ...

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

    一.设置一个新的测试项目 在用google test写测试项目之前,需要先编译gtest到library库并将测试与其链接.我们为一些流行的构建系统提供了构建文件: msvc/ for Visual ...

  9. Google C++单元测试框架GoogleTest---值参数化测试

    值参数化测试允许您使用不同的参数测试代码,而无需编写同一测试的多个副本. 假设您为代码编写测试,然后意识到您的代码受到布尔参数的影响. TEST(MyCodeTest, TestFoo) { // A ...

随机推荐

  1. Lasso回归算法: 坐标轴下降法与最小角回归法小结

    前面的文章对线性回归做了一个小结,文章在这: 线性回归原理小结.里面对线程回归的正则化也做了一个初步的介绍.提到了线程回归的L2正则化-Ridge回归,以及线程回归的L1正则化-Lasso回归.但是对 ...

  2. Oracle常用函数

    前一段时间学习Oracle 时做的学习笔记,整理了一下,下面是分享的Oracle常用函数的部分笔记,以后还会分享其他部分的笔记,请大家批评指正. 1.Oracle 数据库中的to_date()函数的使 ...

  3. 如何用Pivot实现行列转换

    在Oracle中,如果要实现行列转换,较为常见的是用DECODE和CASE语句.对于简单的行列转行,DECODE和CASE语句尚能应付.在逻辑比较复杂,分组聚合较多的场景中,DECODE和CASE语句 ...

  4. 2015-10-22 前思后想,决定重构表结构,免得这个APP死在数据表设计上

    新的设计稿出来了,如下,原来旧的是第二张       -------  原来的评论级数只有2级,现在是不限,2级的意思是,用户评论该帖是一级,用户的评论能被人评论,这是第2级,评论评论的评论不能够再被 ...

  5. 解决ajax跨域请求 (总结)

    ajax跨域请求,目前已用几种方法实现:   1)用原生js的xhr对象实现.                var url="http://freegeoip.net/json/" ...

  6. 用JAVA实现插值查询的方法(算近似值,区间求法)

    插值查询:如果有这样一张表,有一列叫水位,有一列叫库容,比如下面的图. 我现在想做这么一件事情:对于这个测站而言,当我输入某一个水位或者库容的时候,想要查询到对应的水位或者库容呢? 而这个值不一定是存 ...

  7. 项目积累html标签

    今天遇到一个不太常用都标签,网上以后慢慢记下项目中用到都东西. 1.<em> 标签 告诉浏览器把其中的文本表示为强调的内容.对于所有浏览器来说,这意味着要把这段文字用斜体来显示. 在文本中 ...

  8. Django admin 权威指南(一)

    版本: Django 1.10 此部分由官方文档<6.5.1 The Django admin site>翻译而来. 6.5.1.1 概览 默认情况下,使用startproject的时候, ...

  9. CClayer ignoreAnchorPointForPosition 参数的作用

    ignoreAnchorPointForPosition:忽略锚点对于位置的设定.即非位置的设定(比如说缩放),则不受此参数的控制. 并且默认情况下CCLayer的默认锚点是 中点,而不是左下角的点. ...

  10. TypeError: invalid 'in' operand obj

    尝试在程序去访问远程的Web API,它在运行时,出现异常: TypeError: invalid 'in' operand obj 由于从服务器返回的数据是json.当我们需要得到这些数据时,还得需 ...