How to make google-test classes friends with my classes?

前端 未结 4 982
别那么骄傲
别那么骄傲 2021-02-01 01:48

I heard there is a possibility to enable google-test TestCase classes friends to my classes, thus enabling tests to access my private/protected members.

How to accomplis

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 02:15

    Try this (straight from Google Test docs...):

    FRIEND_TEST(TestCaseName, TestName);
    

    For example:

    // foo.h
    #include 
    
    // 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().
    }
    

提交回复
热议问题