简体   繁体   English

Google Test中的FRIEND_TEST - 可能是循环依赖?

[英]FRIEND_TEST in Google Test - possible circular dependency?

I am trying to figure out how FRIEND_TEST works in Google Tests. 我想弄清楚FRIEND_TEST在Google测试中是如何工作的。 https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#testing-private-code https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#testing-private-code

I am looking at the following item, trying to implement it in my code: 我正在查看以下项目,尝试在我的代码中实现它:

// foo.h
#include "gtest/gtest_prod.h"

// Defines FRIEND_TEST.
class Foo {
  ...
 private:
  FRIEND_TEST(FooTest, BarReturnsZeroOnNull);
  int Bar(void* x);
};

// foo_test.cc
...
TEST(FooTest, BarReturnsZeroOnNull) {
  Foo foo;
  EXPECT_EQ(0, foo.Bar(NULL));
  // Uses Foo's private member Bar().
}

In the code above, the piece that I can't see, is that foo_test.cc must include foo.h, in order to have access to Foo and Bar(). 在上面的代码中,我看不到的部分是foo_test.cc必须包含foo.h,以便能够访问Foo和Bar()。 [Perhaps it works differently for Google ? [也许它对Google有不同的作用? in my code, I must include it] 在我的代码中,我必须包含它]

That will result in circular dependency... 这将导致循环依赖......

Am I missing something ? 我错过了什么吗?

Edit: code sample: (re-edited after fixing - solution being changing test file from *.h to *.cpp): 编辑:代码示例:(修复后重新编辑 - 解决方案是将测试文件从* .h更改为* .cpp):

Project ppppp - file myfile.h: 项目ppppp - 文件myfile.h:

class INeedToBeTested
{
public:
  extern friend class blah_WantToTestThis_Test;
  INeedToBeTested();
  INeedToBeTested(INeedToBeTested* item);
  INeedToBeTested(OtherClass* thing, const char* filename);
  ~INeedToBeTested();
  bool Testable();
  std::string MoreTestable();

private:
  int WantToTestThis();
};

Project ppppp_gtest, file myFile_gtest.cpp: 项目ppppp_gtest,文件myFile_gtest.cpp:

#pragma once
#include "gtest/gtest.h"
#include "myfile.h" //property sheet adds include locations
#include "otherclass.h"

  class blah: public ::testing::Test{
  // declarations, SetUp, TearDown to initialize otherclass thing, std::string filename
  }
  TEST_F(blah, WantToTestThis)
      {
        INeedToBeTested item(thing, filename.c_str());
        item.WantToTestThis();   // inaccessible when this content is in header file
      }

During my efforts to get this to work, I also experimented with creating a wrapper class (this also works only if in a cpp, not in header); 在努力实现这一目标的过程中,我还尝试了创建一个包装类(这也适用于cpp中,而不是标题中); while it requires changing private to protected , it doesn't require additional declarations inside tested code which each new test: 虽然它需要将private更改为protected ,但它不需要在每个新测试的测试代码中进行额外的声明:

// option: create wrapper (change private to protected first) 
  class INeedToBeTestedWrapper:public INeedToBeTested 
      {
      public:
         INeedToBeTestedWrapper(OtherClass* thing, std::string filename):
            INeedToBeTested(OtherClass* thing, filename.c_str());
      public:
         using INeedToBeTested::WantToTestThis;
      };

      TEST_F(blah, WantToTestThis)
      {
        INeedToBeTestedWrapper item(thing, filename);
        item.WantToTestThis();   
      }

There shouldn't be a problem here. 这里不应该有问题。

FRIEND_TEST in this case simply defines 在这种情况下, FRIEND_TEST只是定义

friend class FooTest_BarReturnsZeroOnNull_Test;

which is the class ultimately defined by using the TEST macro. 这是通过使用TEST宏最终定义的类。 There's no need to link gtest or any of your test code to the foo library/exe. 没有必要 gtest或任何测试代码链接foo库/ exe。 You only need to #include "gtest/gtest_prod.h" as you have done. 你只需要像你一样#include "gtest/gtest_prod.h"

In foo_test.cc, you need to #include "foo.h" since it's using an actual instance of a Foo object. 在foo_test.cc中,你需要#include "foo.h"因为它使用的是Foo对象的实际实例。 You also need to link your foo library to the test executable, or if foo isn't a library, you need to compile in the foo sources. 您还需要将foo库链接到测试可执行文件,或者如果foo不是库,则需要在foo源代码中进行编译。

So in summary, foo doesn't need any test code with the exception of the tiny gtest_prod.h header, but the test needs linked to the foo code. 总而言之, foo不需要任何测试代码,除了微小的gtest_prod.h头文件,但测试需要链接到foo代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM