[英]How to pass a test fixture to a helper function in C++ GTest
I have a protected static method in my test fixture I wish to call from a helper function, instead of from the unit test function itself.我的测试夹具中有一个受保护的 static 方法,我希望从助手 function 调用,而不是从单元测试 function 本身调用。
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
}
When I try Line 2, I obviously get an error that the method is 'inaccessible' because its a protected method within fixture
.当我尝试第 2 行时,我显然收到一个错误,指出该方法是“不可访问的”,因为它是
fixture
中的一个受保护方法。 How can I somehow pass the test fixture to helper_func
, or else put fixture_func
within the scope of helper_func
?我怎样才能以某种方式将测试夹具传递给
helper_func
,或者将fixture_func
放在 helper_func 的helper_func
中?
If you're wondering, simply calling fixture func
from the unit test itself is not an option because I am designing a test framework meant to simplify the use of fixture_func for a particular purpose.如果您想知道,简单地从单元测试本身调用
fixture func
不是一种选择,因为我正在设计一个旨在简化 fixture_func 用于特定目的的测试框架。 I also do not have the ability to make non-trivial changes to fixture
.我也没有能力对
fixture
进行重要的更改。
You cannot call protected
nor private
methods from outside of a class, no matter if that method is static
or if the calling function is C-style one.您不能从 class 外部调用
protected
或private
方法,无论该方法是static
还是调用 function 是 C 风格的方法。
In any case, you would need the class scope Fixture::
before fixture_func
:在任何情况下,您都需要 class scope
Fixture::
在fixture_func
之前:
void helper_func( int bar ) {
Fixture::fixture_func( bar );
}
You need to make fixture_func
become public
somehow, you may try:你需要让
fixture_func
以某种方式public
,你可以尝试:
class FixtureExpanded : public Fixture { };
void helper_func( int bar ) {
FixtureExpanded::fixture_func( bar );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.