【问题标题】:Both parameterized and sharing recources test in googletestgoogletest 中的参数化和共享资源测试
【发布时间】:2016-11-07 21:27:29
【问题描述】:

使用 googletest 框架,我尝试创建一个继承的夹具类,以便进行参数化和共享资源测试。

class FixtDBadminConnShared : public ::testing::Test {
  public:
    static void SetUpTestCase() {
      shared_conn_ = new ::DB::DB_connection();
    }
    static void TearDownTestCase() {
      delete shared_conn_;
    }
    static ::DB::DB_connection * shared_conn_;
    };
::DB::DB_connection * FixtDBadminConnShared::shared_conn_ = nullptr;

class FixtDBadminConnExec :public FixtDBadminConnShared, public ::testing::TestWithParam<string> 
  {
  protected:
    using FixtDBadminConnShared::SetUpTestCase;
    using FixtDBadminConnShared::TearDownTestCase;

    void SetUp() override {
      query_ = GetParam();
    }
    string query_;
  };       

尝试调用测试:

TEST_P(FixtDBadminConnExec, SelectWithoutParam) {
 //do smth
}
INSTANTIATE_TEST_CASE_P(QueriesOrbital0param, FixtDBadminConnExec,
::testing::Values( string{ "SELECT * from my_table;" }));

我得到下一个错误

Error   C2594   'return': ambiguous conversions from 'FixtDBadminConnExec_SelectWithoutParam_Test *' to 'testing::Test *'   gtest_mytest    e:\libs\googletest\googletest\include\gtest\internal\gtest-param-util.h 415 

这是 gtest-param-util.h 的一部分,在 return new TestClass() 上有 415 行:

template <class TestClass>
class ParameterizedTestFactory : public TestFactoryBase {
 public:
  typedef typename TestClass::ParamType ParamType;
  explicit ParameterizedTestFactory(ParamType parameter) :
      parameter_(parameter) {}
  virtual Test* CreateTest() {
    TestClass::SetParam(&parameter_);
    return new TestClass();
  }

 private:
  const ParamType parameter_;

  GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestFactory);
};

所以,我想这个问题也可能是我想同时使用参数化测试(TEST_P 宏)和共享资源测试(TEST_F 宏)。如果可以,我该怎么做?

【问题讨论】:

    标签: c++ templates googletest static-members


    【解决方案1】:

    您的问题是 TestWithParam 继承自 testing::Test 并且存在模棱两可的转换。相反,继承自 WithParamInterface

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 2023-03-09
      • 2011-01-15
      • 1970-01-01
      • 2015-06-15
      相关资源
      最近更新 更多