参数化和共享资源测试在googletest

Both parameterized and sharing recources test in googletest

本文关键字:googletest 测试 共享资源 参数      更新时间:2023-10-16

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

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:libsgoogletestgoogletestincludegtestinternalgtest-param-util.h 415 

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

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宏)。如果可以,我怎么能做到呢?

这里的问题是TestWithParam<>继承自Test::Test,并且有一个不明确的转换。相反,从WithParamInterface<>

继承