How to write tests with parameter parameters for values ​​in a non-stationary container?

I am trying to write a test with parameterization of values, where test values ​​are created only after test classes have been created, i.e. test values ​​are stored in a non-static variable. This means that I cannot do what I usually do when the container is static:

INSTANTIATE_TEST_CASE_P(SomeCriteria, SomeTest,
                    ValuesIn(SomeClass::staticContainerWithTestINputs) );

Here is an example of MVCE when I was stuck:

#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace testing;

// This is not a test class, so I can't modify `myInt` to be static just so
// that I can write tests.
struct CustomClass
{
  int myInt = 0;
};

class Fixture : public ::testing::Test {
protected:
  CustomClass myCustomCls;

  virtual void SetUp() override
  {
    // This variable needs to be used in the parameterized test.
    myCustomCls.myInt = 42;
  }
};

class ValueParamTest : public Fixture, public WithParamInterface<int> {
public:
  // The container holding the values to be tested.
  const std::vector<int> validInputs {
    1, 24, myCustomCls.myInt
  };

protected:
  virtual void SetUp()
  {
    Fixture::Fixture::SetUp();
    mTestInput = GetParam();
  }

  int mTestInput;
};


TEST_P(ValueParamTest, ValidInputs)
{
  EXPECT_TRUE(mTestInput < 100);
}

// COMPILER ERROR HERE
INSTANTIATE_TEST_CASE_P(ValidInputValues, ValueParamTest,
                        ValuesIn(ValueParamTest::validInputs) );

Compiler Error:

59: error: invalid use of non-static data member ‘ValueParamTest::validInputs’
                         ValuesIn(ValueParamTest::validInputs) );
                                                  ^

There is no instance of this class ValueParamTest, so I cannot access its instance data members or member functions.

Can anyone give a hint how this can be done in GTest?

+4
source share
2 answers

, Googletest , , : -

main.cpp

#include <gtest/gtest.h>
#include <functional>
#include <memory>
using namespace testing;

struct CustomClass
{
    int myInt = 0;
};

class Fixture : public ::testing::Test {
protected:
    static std::shared_ptr<CustomClass> & getSpecimen() {
        static std::shared_ptr<CustomClass> specimen;
        if (!specimen) {
            specimen.reset(new CustomClass{42});
        }
        return specimen;
    }
    void TearDown() override
    {
        getSpecimen().reset();
    }
};

class ValueParamTest : 
    public Fixture, public WithParamInterface<std::function<int()>> {
public:
    static std::vector<std::function<int()>> validInputs;

protected:
    void SetUp() override {
        mTestInput = GetParam()();
    }
    void TearDown() override {
        Fixture::TearDown();
    }

    int mTestInput;
};

std::vector<std::function<int()>> ValueParamTest::validInputs{
    []() { return 1; },
    []() { return 24; },
    []() { return ValueParamTest::getSpecimen()->myInt; }
}; 


TEST_P(ValueParamTest, ValidInputs)
{
    std::cout << "mTestInput = " << mTestInput << std::endl;
    EXPECT_TRUE(mTestInput < 100);
}

INSTANTIATE_TEST_CASE_P(ValidInputValues, ValueParamTest,
                        ValuesIn(ValueParamTest::validInputs) );

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

:

g++ -Wall -std=c++14 -o gtestrun main.cpp -lgtest -pthread && ./gtestrun 
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from ValidInputValues/ValueParamTest
[ RUN      ] ValidInputValues/ValueParamTest.ValidInputs/0
mTestInput = 1
[       OK ] ValidInputValues/ValueParamTest.ValidInputs/0 (0 ms)
[ RUN      ] ValidInputValues/ValueParamTest.ValidInputs/1
mTestInput = 24
[       OK ] ValidInputValues/ValueParamTest.ValidInputs/1 (1 ms)
[ RUN      ] ValidInputValues/ValueParamTest.ValidInputs/2
mTestInput = 42
[       OK ] ValidInputValues/ValueParamTest.ValidInputs/2 (0 ms)
[----------] 3 tests from ValidInputValues/ValueParamTest (1 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (1 ms total)
[  PASSED  ] 3 tests.
+2

, TEST_P , INSTANTIATE_TEST_CASE_P - , . / , , - SUT for :

using namespace testing;

// This is not a test class, so I can't modify `myInt` to be static just so
// that I can write tests.
struct CustomClass
{
  int myInt = 0;
};

class Fixture : public ::testing::Test {
protected:
  CustomClass myCustomCls;

  virtual void SetUp() override
  {
    // This variable needs to be used in the parameterized test.
    myCustomCls.myInt = 42;
  }
};

class ValueParamTest : public Fixture {
public:
  // The container holding the values to be tested.
  const std::vector<int> validInputs {
    1, 24, myCustomCls.myInt, 101, 99, 102
  };

protected:
  virtual void SetUp()
  {
    Fixture::Fixture::SetUp();
  }
};


TEST_F(ValueParamTest, ValidInputs)
{
    std::for_each(validInputs.begin(), validInputs.end(),
        [](int v){ EXPECT_TRUE(v < 100) << "invalid input: " << v; });
}

, ( ).

, , . .

+1

Source: https://habr.com/ru/post/1693625/


All Articles