简体   繁体   English

如何将测试夹具传递给 C++ GTest 中的助手 function

[英]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 外部调用protectedprivate方法,无论该方法是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.

相关问题 如何通过回调在 C++ 中做 Gtest - How to pass Callback to do Gtest in c++ 如何在C ++中使用gtest / gmock调用另一个全局函数的地方编写针对全局函数的单元测试? - How to write unit test for global functions where it is calling another global function using gtest/gmock in C++? 如何在 C++ 中使用 gtest 测试具有不同构造函数的多个接口实现? - How to test several interface implementations with different constructors with gtest in C++? 如何在Google Test(gtest)中使用fixture成员值运行参数化测试? - How to run Parameterized Tests with fixture member values in Google Test (gtest)? c++ Google test (gtest):如何创建自定义断言和期望? - c++ Google test (gtest): how to create custom asserts and expects? 如何在C ++ Gtest中测试输入和输出重载运算符 - How to test input and output overloaded operator in C++ Gtest 如何使用GTest测试具有多个模板参数的C ++模板类? - How to test C++ template class with multiple template parameters with GTest? C ++辅助函数 - C++ helper function 如何使用gtest测试带有多个模板参数的c ++模板类? - How to test c++ template class with multiple template parameters using gtest? 无法将二维数组传递给 C++ 中的辅助函数 - Cannot pass 2d array into a helper function in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM