Google C++单元测试框架---Google TestExtending Google Test by Handling Test Events

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*/) {}
};

  

时间: 2024-08-09 17:08:55

Google C++单元测试框架---Google TestExtending Google Test by Handling Test Events的相关文章

Google C++单元测试框架

一.概述 Google C++单元测试框架(简称Gtest),可在多个平台上使用(包括Linux, Mac OS X, Windows, Cygwin和Symbian),它提供了丰富的断言.致命和非致命失败判断,能进行值参数化测试.类型参数化测试."死亡测试".Gtest是一个开源的项目,其源码可以从这里下载,目前的代码发行版是1.6.0. 编译 源码包中的README文件说明了如何编译Gtest源码,目录msvc.xcode中分别包含了Windows.Mac OS X平台相关的项目文

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

如果你还没有搭建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

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

本文是gtest高级测试指南的译文,由于文章太长,分上下两部分. 一.简介 本文档将向您展示更多的断言,以及如何构造复杂的失败消息,传播致命的故障,重用和加速您的测试夹具,并在您的测试使用各种标志. 二.更多断言 本节包括一些不太常用,但仍然重要的断言. 2.1 显式成功和失败 这三个断言实际上不测试值或表达式. 相反,它们直接产生成功或失败. 与实际执行测试的宏类似,您可以将自定义失败消息流入它们. SUCCEED(); 生成成功. 这不会使整体测试成功. 只有当测试在其执行期间没有任何断言失

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

一.设置一个新的测试项目 在用google test写测试项目之前,需要先编译gtest到library库并将测试与其链接.我们为一些流行的构建系统提供了构建文件: msvc/ for Visual Studio, xcode/ for Mac Xcode, make/ for GNU make, codegear/ for Borland C++ Builder. 如果你的构建系统不在这个名单上,在googletest根目录有autotools的脚本(不推荐使用)和CMakeLists.txt

Google C++单元测试框架---TestFixture使用

一.测试夹具(Test Fixtures):对多个测试使用相同的数据配置 如果你发现自己写了两个或更多的测试来操作类似的数据,你可以使用测试夹具.它允许您为几个不同的测试重复使用相同的对象配置. 要创建夹具,只需: 1.从:: testing :: Test派生一个类. 使用protected:或public:开始它的主体,因为我们想从子类     访问fixture成员.  2.在类中,声明你打算使用的任何对象.  3.如果需要,可以编写默认构造函数或SetUp()函数来为每个测试准备对象.

玩转Google开源C++单元测试框架Google Test系列(gtest)之一 初识gtest

进入文件夹执行: ./configure make make install 完毕即可正常使用: (1)包含include目录 -I/root/scp/gtest/gtest-1.3.0: (2)包含lib中的动态链接库:-lgtest -L/root/scp/gtest/gtest-1.3.0/lib 示例代码: [cpp] view plaincopy #include <gtest/gtest.h> int Foo(int a, int b) { if (a == 0 || b == 0

Google开源C++单元测试框架Google Test

1.玩转Google开源C++单元测试框架Google Test系列(gtest)之一 - 初识gtest 2.玩转Google开源C++单元测试框架Google Test系列(gtest)之二 - 断言 3.玩转Google开源C++单元测试框架Google Test系列(gtest)之三 - 事件机制 4.玩转Google开源C++单元测试框架Google Test系列(gtest)之四 - 参数化 5.玩转Google开源C++单元测试框架Google Test系列(gtest)之五 -

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

越来越多公司采用敏捷开发,单元和回归测试越来越重要,GTest作为最佳C++单元测试工具越来越多的被使用.转自 http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 前段时间学习和了解了下Google的开源C++单元测试框架Google Test,简称gtest,非常的不错. 我们原来使用的是自己实现的一套单元测试框架,在使用过程中,发现越来越多使用不便之处,而这样不便之处,gtest恰恰很好的解决了. 其实gtest本身的

转:玩转Google开源C++单元测试框架Google Test系列

转自http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 前段时间学习和了解了下Google的开源C++单元测试框架Google Test,简称gtest,非常的不错. 我们原来使用的是自己实现的一套单元测试框架,在使用过程中,发现越来越多使用不便之处,而这样不便之处,gtest恰恰很好的解决了. 其实gtest本身的实现并不复杂,我们完全可以模仿gtest,不断的完善我们的测试框架, 但最后我们还是决定使用gtest取代掉