gpt4 book ai didi

c++ - 使用CRTP和Google测试框架的库API

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:07 26 4
gpt4 key购买 nike

我有一个使用CRTP的模板库

// Base configuration to be derived from.
struct TemplateBaseConfig {
static constexpr auto config_1 = true;
static constexpr auto config_2 = false;
static constexpr auto config_val_3 = 42;
};

template <typename Derived_, typename DerivedConfig_, typename... Widgets>
struct TemplateBaseClass {
std::tuple<Widgets...> widgets;
// Lots of functions and logic etc.
void function_1() {
if constexpr (DerivedConfig_::config_1) {
// do something
}
static_cast<Derived_*>(this)->function_2();
}
void function_2() { /*do something*/ }
};


现在,用户将 TemplateBaseClass用作:

// Make a custom config.
struct DerivedConfig : TemplateBaseConfig {
static constexpr auto config_1 = false;
};

struct DerivedClass : TemplateBaseClass<DerivedClass, DerivedConfig, Widget1, Widget2> {
void function_2() { /* override base logic */ }
};


我通过继承 TemplateBaseClassMockTemplateBaseClassInterface编写单元测试。

template <typename TestingClass, typename TestingClassConfig, typename... W>
struct MockTemplateBaseClassInterface : TemplateBaseClass<TestingClass, TestingClassConfig, W> {
// Mock some internal functions which always need to be mocked.
MOCK(internal_function_1);
MOCK(internal_function_2);
// Some custom setup for unittests like override the log dir
void function_2() { Parent::function_2(); MakeLogDir(); } // Parent is this guy's base class.
}


为给定的 DerivedConfig编写单元测试,因此我从 MockTemplateBaseClassInterface创建一个派生类:

ClassToTestFunction1 : MockTemplateBaseClassInterface<ClassToTestFunction1, TestConfig1, MockWidget> {
// Write more mock functions if required.
MOCK(function3);
};


然后,在夹具类中使用 ClassToTestFunction1编写单元测试。

这可以完美测试 TemplateBaseClass的任何内容,因为我可以给出任何配置,并在需要时编写自己的模拟。

现在,如果我必须向最终用户公开此测试框架,以便他可以为 DerivedClass编写测试,那么前进的方向应该是什么?

我可以要求用户执行以下操作:

    struct DerivedClass :
#ifdef _DOING_TESTING_
MockTemplateBaseClassInterface<DerivedClass, DerivedConfig, Widget1, Widget2>
#else
TemplateBaseClass<DerivedClass, DerivedConfig, Widget1, Widget2>
#endif
{
void function_2() { /* override base logic */ }
};


但是现在用户无法真正模拟 TemplateBaseClass的功能。

用户将要测试他的 function_2()。但是,如果 function_2()使用 TemplateBaseClass的某些功能,他将想对其进行模拟。我的意思是,这就是单元测试的全部内容吧?

最佳答案

您的方法太复杂了。

TemplateBaseClass类模板不需要Derived_DerivedConfig是特定的东西-例如从TemplateBaseConfig派生。

只需定义TemplateBaseClass是完全测试特定的类-我的意思是-与项目中的其他任何类隔离地测试TemplateBaseClass

struct TemplateBaseClass_TestConfig
{
bool config_1;
bool config_2;
int config_val_3;
};
struct TemplateBaseClass_TestDerived
{
MOCK_METHOD0(function_1, void());
MOCK_METHOD0(function_2, void());
};


class TemplateBaseClassTestSuite : public ::testing::Test
{
public:
using ClassUnderTest = TemplateBaseClass<TemplateBaseClass_TestConfig
TemplateBaseClass_TestDerived
/* specify some extra WidgetMocks if needed */
>;
ClassUnderTest objectUnderTest{};
TemplateBaseClass_TestDerived& derivedMock = objectUnderTest;
};


然后您的测试就这么简单:

TEST_F(TemplateBaseClassTestSuite, function1CallsDerivedFunction1IfConfig1Allows)
{
objectUnderTest.config_1 = true;
EXPECT_CALL(derivedMock, function_1());
objectUnderTest.function_1();
// of course the above is just example - I have no idea if this is correct behaviour for your real class
}


因此-如您所见-无需使用测试机制来污染客户端类。测试应透明。

关于c++ - 使用CRTP和Google测试框架的库API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50903831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com