c++c++20googletestparameterized-unit-test

C++ Google Type-Parameterized and Value-Parameterized Tests combined?


I would like to be able to parameterize my tests for various types through the following code snippet:

template <typename T>
class MyTestSuite : public testing::TestWithParam<tuple<T, vector<vector<T>>, T, T>>
{
public:
    MyTestSuite()
    {
        _var = get<0>(GetParam());
        // and the rest of the test params
    }
protected:
    T _var;
};
TYPED_TEST_SUITE_P(MyTestSuite);
TYPED_TEST_P(MyTestSuite, MyTests)
{
    ASSERT_EQ(this->_expected, this->DoSomething(this->_var));
}
REGISTER_TYPED_TEST_SUITE_P(MyTestSuite,
                            MyTests);
using MyTypes = ::testing::Types<size_t>;
INSTANTIATE_TYPED_TEST_SUITE_P(
    MyGroup,
    MyTestSuite,
    ::testing::Combine(
        ::testing::Values(4, vector<vector<size_t>>{{1, 2}, {1, 3}, {3, 4}}, 1, 1),
        ::testing::Values(10, vector<vector<size_t>>{{1, 2}, {1, 3}, {3, 4}}, 1, 2),
        ::testing::Values(20, vector<vector<size_t>>{{1, 2}, {1, 3}, {3, 4}}, 1, 4)));

and bump into the following compilation error:

error: there are no arguments to ‘GetParam’ that depend on a template parameter, so a declaration of ‘GetParam’ must be available [-fpermissive]
[build]   284 |                 _var = get<0>(GetParam());

This post has been quite some time: Google Test: Is there a way to combine a test which is both type parameterized and value parameterized? Is it still not possible to kill these 2 birds with one stone, judging from the compilation error above?


Solution

  • Decided to use a template base test fixture class with parameterized tests:

    template <typename T>
    class MyTestFixture
    {
    protected:
       void SetUp(...) {}
       T _var;
    };
    class MyTestSuite1 : public MyTestFixture<size_t>, public testing::TestWithParam<tuple<size_t, vector<vector<size_t>>, size_t, size_t>>
    {
    public:
        void SetUp() override
        {
            MyTestFixture::SetUp(get<0>(GetParam()));
            // and the rest of the test params
        }
    };
    class MyTestSuite2 : public MyTestFixture<long>, public testing::TestWithParam<tuple<size_t, vector<vector<long>>, size_t, long>>
    {
    public:
        void SetUp() override
        {
            MyTestFixture::SetUp(get<0>(GetParam()));
            // and the rest of the test params
        }
    };
    
    TEST_P(MyTestSuite1, MyTests1)
    {
        ASSERT_EQ(this->_expected, this->DoSomething(this->_var));
    }
    TEST_P(MyTestSuite2, MyTests2)
    {
        ASSERT_EQ(this->_expected, this->DoSomething(this->_var));
    }
    INSTANTIATE_TEST_SUITE_P(
        MyGroup,
        MyTestSuite1,
        ::testing::Values(make_tuple<size_t, ...>(4, vector<vector<size_t>>{{1, 2}, {1, 3}, {3, 4}}, 1, 1),
            make_tuple<size_t, ...>(10, vector<vector<size_t>>{{1, 2}, {1, 3}, {3, 4}}, 1, 2),
            make_tuple<size_t, ...>(20, vector<vector<size_t>>{{1, 2}, {1, 3}, {3, 4}}, 1, 4)));