简体   繁体   中英

How to use parameterized test cases?

I'm trying to use parameterized tests with a class that takes a POD as parameter. 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() ?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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