简体   繁体   English

如何使用参数化测试用例?

[英]How to use parameterized test cases?

I'm trying to use parameterized tests with a class that takes a POD as parameter. 我正在尝试对带POD作为参数的类使用参数化测试。 I've sort of reached this stage: 我已经达到了这个阶段:

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());

My question is, how to pass the values I need in parameters via INSTANTIATE_TEST_CASE_P and how to I pass a valid instance of parameters to make_connection() ? 我的问题是,如何通过INSTANTIATE_TEST_CASE_P传递parameters需要的值,以及如何将有效的parameters实例传递给make_connection()

It looks like you should be doing something along the lines of 看来您应该按照以下方式进行操作

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

Or you could declare a std::vector<parameters> as a global somewhere that you dynamically could compute, and then pass iterators of that vector to ::testing::Values() 或者,您可以将std::vector<parameters>为可以动态计算的全局变量,然后将该向量的迭代器传递给::testing::Values()

Also, note you wouldn't need the member params in your fixture class, since the parameter is going to be fed automatically by Google Test through GetParam() 另外,请注意,您不需要夹具类中的成员params ,因为Google Test将通过GetParam()自动提供参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM