如何在C++GTest中将测试夹具传递给辅助函数



我的测试夹具中有一个受保护的静态方法,我希望从辅助函数调用,而不是从单元测试函数本身调用。

class Fixture
{
...
protected:
static void fixture_func( int foo );
};
void helper_func( int bar ) {
Fixture::fixture_func( bar );
}
TEST_F( Fixture, example_test ) {
fixture_func( 0 );  //Line 1: This is how you would normally call the method

helper_func( 0 );  //Line 2: This is how I need to call the method
}

当我尝试第2行时,我显然得到了一个错误,即该方法"不可访问",因为它是fixture中受保护的方法。我如何以某种方式将测试夹具传递给helper_func,或者将fixture_func置于helper_func的范围内?

如果你想知道,简单地从单元测试本身调用fixture func是不可行的,因为我正在设计一个测试框架,旨在简化fixture_func用于特定目的的使用。我也没有能力对fixture进行非琐碎的更改。

无论该方法是static还是调用函数是C样式的,都不能从类外部调用protectedprivate方法。

在任何情况下,在fixture_func:之前都需要类作用域Fixture::

void helper_func( int bar ) {
Fixture::fixture_func( bar );
}

你需要以某种方式使fixture_func变成public,你可以尝试:

class FixtureExpanded : public Fixture { };
void helper_func( int bar ) {
FixtureExpanded::fixture_func( bar );
}

最新更新