c++ - 如何为非静态容器中的值编写值参数化测试?

标签 c++ unit-testing googletest

我正在尝试编写一个值参数化测试,其中仅在实例化测试类后才创建测试值,即测试值存储在非静态变量中。 这意味着我不能做我通常做的事情,容器是静态的:

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

这是我遇到困难时的 MVCE 示例:

#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) );

编译错误:

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

ValueParamTest 类没有实例,因此我无法访问它的实例数据成员或成员函数。

任何人都可以提示如何在 GTest 中完成此操作?

最佳答案

看似 Googletest 的宏库没有达到您的要求,但是通过 The Fundamental Theorem of Software Engineering , 你可以这样做:-

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.

关于c++ - 如何为非静态容器中的值编写值参数化测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48785165/

相关文章:

c++ - 如何生成升序随机整数列表

unit-testing - 发送带有 session 数据的模拟环请求

c++ - 找不到 gtest.h 文件 googletest xcode 7.0

c++ - 在 googletest 中循环测试用例

C++ 深度复制链表

c++ - yaml-cpp 甚至为 const 节点修改底层容器?

javascript - sinon stub 无法识别对 stub 的调用

javascript - getByText 用于通过动态生成的字符串将文本拆分为单独的行

c++ - 如何在 Google C++ 测试框架中发送自定义消息?

c++ - 我的编译器应该针对哪些后端语言?