【问题标题】:GoogleTest Parameterized Test - Possible To Call SetUp And TearDown Between Parameters?GoogleTest 参数化测试 - 可以在参数之间调用 SetUp 和 TearDown?
【发布时间】:2015-06-15 09:03:34
【问题描述】:

我有一个 gtest 参数化类,我想在每个参数之间调用一些 SetUp 和 TearDown。我知道 googletest 提供在每个测试用例之前的 SetUp 和在所有测试用例之前的 SetUpTestCase。

我有这样的事情:

class MyParameterizedTest: public TestWithParam<MyParams>
{
public:
   MyParameterizedTest() {}

   void SetUp()
   {
      //called before every test case
   }

   void TearDown()
   {
      //called after every test case
   }

   static void SetUpTestCase()
   {
      //called at the begining of framework and before all test cases
   }

   static void TearDownTestCase()
   {
      //called at the end of the framework and after all test cases
   }

   //Wishing for something like:
   // void SetUpParameter()
   {
      //called before start of parameter
   }
};

INSTANTIATE_TEST_CASE_P(RegistrationTest, InterfaceTest, ValuesIn(AllTheValues::GetAllMyParams()));

有什么想法可以使这项工作有效吗?也许是一种查看特定参数的最后一个测试用例何时运行的方法?还是我必须为每个单独的参数实例化一个测试用例?

【问题讨论】:

  • 您确定您的代码不起作用吗?每个类的静态函数是独立的,而不是每个类模板。
  • 澄清一下:你说的是type-parametrized tests
  • 是类型参数化测试。很好的澄清。我不确定测试类的底层模板是如何工作的,但我确信静态 SetUpTestCase 和 TearDownTestCase 只为我的全套测试用例调用一次

标签: c++ unit-testing googletest parameterized-unit-test


【解决方案1】:

我认为创建这样的组合是违反良好做法的,因为:

可能的解决方案: - 创建单独的测试用例。有用。违背框架几乎总是一条糟糕的路线。如果框架发生变化怎么办?您可能必须对齐所有测试......这在生产代码中确实是错误的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    相关资源
    最近更新 更多