1、前言
有时为了完整的测试一个函数的正确性,我们需要测试给定各种不同输入的情况下,函数的输出是否正确。最直接的方法就是我们手动去给不同输入,然后测试输出是否正确。但是这种方式工作量大,并且都是重复性,无意义的操作。为此,gtest中提供了参数化的测试。
2、参数化测试使用方法
要使用参数化测试,我们需要创建一个子类继承testing::TestWithParam<T>,此处T为我们的参数类型。首先我们看一下这个类的声明:
template <typename T>
class TestWithParam : public Test, public WithParamInterface<T> {
};
它是一个继承自testing::Test类和testing::WithParamInterface<T>的类,接下来我们再看看testing::WithParamInterface<T>这个接口的声明:
template <typename T>
class WithParamInterface {
public:
typedef T ParamType;
virtual ~WithParamInterface() {}
// The current parameter value. Is also available in the test fixture's
// constructor. This member function is non-static, even though it only
// references static data, to reduce the opportunity for incorrect uses
// like writing 'WithParamInterface<bool>::GetParam()' for a test that
// uses a fixture whose parameter type is int.
const ParamType& GetParam() const {
GTEST_CHECK_(parameter_ !&