Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8780871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:09:48+00:00 2026-06-13T20:09:48+00:00

I am trying to figure out how FRIEND_TEST works in Google Tests. https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#testing-private-code I

  • 0

I am trying to figure out how FRIEND_TEST works in Google Tests.
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(). [Perhaps it works differently for 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):

Project ppppp – file 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:

#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); while it requires changing private to protected, it doesn’t require additional declarations inside tested code which each new test:

// 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();   
      }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:09:49+00:00Added an answer on June 13, 2026 at 8:09 pm

    There shouldn’t be a problem here.

    FRIEND_TEST in this case simply defines

    friend class FooTest_BarReturnsZeroOnNull_Test;
    

    which is the class ultimately defined by using the TEST macro. There’s no need to link gtest or any of your test code to the foo library/exe. You only need to #include "gtest/gtest_prod.h" as you have done.

    In foo_test.cc, you need to #include "foo.h" since it’s using an actual instance of a Foo object. 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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to figure out how I can do this properly. The print_r looks like
trying to figure out why this is happening - I have an input text
Trying to figure out why my silverlight app suddenly just displays nothing (right click
Trying to figure out how to type (via events not set the value) on
Trying to figure out a regex for validating a network path ie: \\comp\xyz or
Trying to figure out why my recipient multiselect isn't validating on form submission. Should
Trying to figure out why File Attachment isn't showing on a custom content type.
Trying to figure out the best way to set up collection lists for users
Trying to figure out which to use.
Trying to figure out how to programmatically access the web pages in the application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.