0

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

1

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

I don't want to create a constructor for my POD. Is it possible to create the object in place?
This is what the aggregate construction does in my example. Assuming your type is defined as this: struct parameters { std::string username; std::string host; }; You can simply construct an instance by writing parameters{"username", "192.168.0.254"}
I can get aggregate initialisers to work in general, just not on Google test.
I just tried it on a simple example, and it works for me. What compiler are you using? Have you passed any flag to it to enable at least C++11 in your test project? What is the actual error message?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.