如何使用参数化测试用例

How to use parameterized test cases?

本文关键字:测试用例 参数 何使用      更新时间:2023-10-16

我正在尝试将参数化测试与将 POD 作为参数的类一起使用。我已经到了这个阶段:

struct TestParameters : public ::testing::TestWithParam<parameters> {
  parameters params;
  virtual void SetUp() {
    params.username = "username";
    params.host = "192.168.0.254";
  }
};
TEST_P(TestParameters, connect) {
  std::error_code ec;
  std::unique_ptr<connection> connection = make_connection(GetParam(), ec);
  ASSERT_FALSE(ec);
  ec = connection->connect();
  ASSERT_FALSE(ec);
}
INSTANTIATE_TEST_CASE_P(postgresql_tcp, connection, ::testing::Values());

我的问题是,如何通过INSTANTIATE_TEST_CASE_P传递我需要的值parameters以及如何将有效的parameters实例传递给make_connection()

看起来你应该做一些类似的事情

INSTANTIATE_TEST_CASE_P(postgresql_tcp, connect,
                        ::testing::Values(parameters{"username", "192.168.0.254"}
                                      //, parameters{ other params here }
                                          ));

或者,您可以将std::vector<parameters>声明为可以动态计算的全局,然后将该向量的迭代器传递给::testing::Values()

另外,请注意,您的夹具类中不需要杆件params,因为该参数将由 Google Test 通过 GetParam() 自动提供