我试图编写一个值参数化测试,其中测试值仅在测试类被实例化后创建,即测试值存储在非静态变量中。 这意味着我不能做我通常做的事情,容器是静态的:
[
{
"children_count": 0,
"id": 1
},
{
"children_count": 0,
"id": 2
},
{
"children_count": 0,
"id": 3
}
]
这是我遇到的一个MVCE示例:
INSTANTIATE_TEST_CASE_P(SomeCriteria, SomeTest,
ValuesIn(SomeClass::staticContainerWithTestINputs) );
编译错误:
#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) );
^
类的实例,因此我无法访问其实例数据成员或成员函数。
任何人都可以暗示如何在GTest中完成这项工作?
答案 0 :(得分:3)
看似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.
答案 1 :(得分:1)
我认为可能无法使用动态生成的值来实例化TEST_P
,因为INSTANTIATE_TEST_CASE_P
是一个实际为每个参数值定义新函数的宏。我想到的唯一解决方案/解决方法就是在正常测试中检查for循环中SUT的所有输入:
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; });
}
当然,它只会被视为一个具有所有缺点(和优点)的测试用例。
如果我错了,请告诉我。生成动态参数化测试用例会很有趣。