0

When passing values into a Parameterised Test using Google Test:

INSTANTIATE_TEST_SUITE_P(InstantiationName,
                         FooTest,
                         testing::Values("meeny", "miny", "moe"));

is there anyway to construct more c, such as a vector, before passing them into testing::Values?

pingu
  • 8,719
  • 12
  • 50
  • 84

1 Answers1

2

You can pass many different types to the parametrized types, e.g. vector:

struct VectorTest : public testing::TestWithParam<std::vector<int>> {};

TEST_P(VectorTest, MyTestCase) {
    auto expectedVector = std::vector<int>{42, 314, 271, 161};
    ASSERT_EQ(expectedVector, GetParam());
}

INSTANTIATE_TEST_CASE_P(VectorInstantiationName,
                         VectorTest,
                         testing::Values(std::vector<int>{42, 314, 271, 161}));

or user-defined types:

struct MyParam {
    int i;
    std::string s;
};

struct MyTest : public testing::TestWithParam<MyParam> {};

TEST_P(MyTest, MyTestCase) {
    ASSERT_EQ(42, GetParam().i);
    ASSERT_EQ("foo", GetParam().s);
}

INSTANTIATE_TEST_CASE_P(InstantiationName,
                         MyTest,
                         testing::Values(MyParam{42, "foo"}));

(using INSTANTIATE_TEST_CASE_P as I'm on 1.8 currently; INSTANTIATE_TEST_SUITE_P shall be used for newer versions of gtest).

Quarra
  • 2,527
  • 1
  • 19
  • 27
  • Thanks for the examples @Quarra, I was wanting to build up layers of objects before passing into testing::Value, and not be limited to constructing these objects inline within testing::Values. Is it possible to learn this power? – pingu May 25 '20 at 10:09
  • @pingu, sorry, I must've misunderstood the question. Can you elaborate what exactly is needed? Given `MyParam` is more complicated type, it's construction can be handled by user-defined ctor for this struct. If you want to run tests on some global variables, use `testing::ValuesIn`. If you need to generate different possible combinations, use `::testing::Combine` and `std::tuple` instead `MyParam`; see https://stackoverflow.com/questions/6256758/can-googletest-value-parameterized-with-multiple-different-types-of-parameters. – Quarra May 26 '20 at 06:09