测试夹具中的设置与构造方法 [英] SetUp vs Constructor in Test Fixture

查看:83
本文介绍了测试夹具中的设置与构造方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么测试夹具在Google Test中具有SetUp方法?构造函数实际上不是一回事吗?对于TearDown方法也是如此.对SetUp和Constructor以及TearDown和Destructor的调用都与TestEventListeners一致:OnTestStart和OnTestEnd.

Why does a test fixture have a SetUp method in Google Test? Isn't the Constructor effectively the same thing? Likewise for the TearDown method. Calls to both SetUp and the Constructor, as well as TearDown and the Destructor, are consistent with the TestEventListeners: OnTestStart and OnTestEnd.

推荐答案

FAQ中的答案:

我应该使用测试治具的构造器/析构器还是设置/拆卸功能?

要记住的第一件事是googletest不会重复使用同一测试 多个测试中的夹具对象.对于每个TEST_F,googletest将创建 一个新鲜测试夹具对象,立即调用SetUp(),运行测试主体, 调用TearDown(),然后删除测试治具对象.

Should I use the constructor/destructor of the test fixture or the set-up/tear-down function?

The first thing to remember is that googletest does not reuse the same test fixture object across multiple tests. For each TEST_F, googletest will create a fresh test fixture object, immediately call SetUp(), run the test body, call TearDown(), and then delete the test fixture object.

当您需要编写每次测试设置和拆卸逻辑时,您可以选择 使用测试治具的构造器/析构器或SetUp()/TearDown()之间. 通常首选前者,因为它具有以下优点:

When you need to write per-test set-up and tear-down logic, you have the choice between using the test fixture constructor/destructor or SetUp()/TearDown(). The former is usually preferred, as it has the following benefits:

  • 通过在构造函数中初始化成员变量,我们可以选择 将其设置为const,有助于防止其值意外更改,并且 使测试更明显地正确.
  • 如果我们需要将测试治具类细分为子类,则该子类为 确保构造函数调用基类的构造函数 first ,并且 保证子类的析构函数调用基类的析构函数 之后.使用SetUp()/TearDown(),子类可能会犯以下错误: 忘记调用基类的SetUp()/TearDown()或在 错误的时间.
  • By initializing a member variable in the constructor, we have the option to make it const, which helps prevent accidental changes to its value and makes the tests more obviously correct.
  • In case we need to subclass the test fixture class, the subclass' constructor is guaranteed to call the base class' constructor first, and the subclass' destructor is guaranteed to call the base class' destructor afterward. With SetUp()/TearDown(), a subclass may make the mistake of forgetting to call the base class' SetUp()/TearDown() or call them at the wrong time.

在以下少数情况下,您可能仍想使用SetUp()/TearDown():

  • 在构造函数(或析构函数)的主体中,无法使用 ASSERT_xx宏.因此,如果进行设置操作可能会导致致命事故 测试失败应阻止测试运行,因此有必要 使用CHECK宏或使用SetUp()代替构造函数.
  • 如果拆卸操作可能引发异常,则必须使用 TearDown()与析构函数相反,因为析构函数的抛出导致 行为不确定,通常会立即杀死您的程序.笔记 许多标准库(例如STL)在发生异常时可能会抛出 在编译器中启用.因此,如果您喜欢TearDown() 想要编写可有或没有例外的便携式测试.
  • googletest团队正在考虑使用断言宏 启用了例外的平台(例如Windows,Mac OS和Linux) 客户端),这将消除用户传播的需求 从子例程到调用者的失败.因此,您不应该使用 googletest在析构函数中的断言,如果您的代码可以在这样的环境下运行 平台.
  • 在构造函数或析构函数中,不能在以下位置调用虚拟函数 这个对象. (您可以调用一个声明为virtual的方法,但是它将被 静态绑定.)因此,如果您需要调用 在派生类中重写,您必须使用SetUp()/TearDown().
    • In the body of a constructor (or destructor), it's not possible to use the ASSERT_xx macros. Therefore, if the set-up operation could cause a fatal test failure that should prevent the test from running, it's necessary to use a CHECK macro or to use SetUp() instead of a constructor.
    • If the tear-down operation could throw an exception, you must use TearDown() as opposed to the destructor, as throwing in a destructor leads to undefined behavior and usually will kill your program right away. Note that many standard libraries (like STL) may throw when exceptions are enabled in the compiler. Therefore you should prefer TearDown() if you want to write portable tests that work with or without exceptions.
    • The googletest team is considering making the assertion macros throw on platforms where exceptions are enabled (e.g. Windows, Mac OS, and Linux client-side), which will eliminate the need for the user to propagate failures from a subroutine to its caller. Therefore, you shouldn't use googletest assertions in a destructor if your code could run on such a platform.
    • In a constructor or destructor, you cannot make a virtual function call on this object. (You can call a method declared as virtual, but it will be statically bound.) Therefore, if you need to call a method that will be overridden in a derived class, you have to use SetUp()/TearDown().
    • 这篇关于测试夹具中的设置与构造方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆