Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do I test private class members without writing FRIEND_TEST()s?

0
Posted

How do I test private class members without writing FRIEND_TEST()s?

0

You should try to write testable code, which means classes should be easily tested from their public interface. One way to achieve this is the Pimpl idiom: you move all private members of a class into a helper class, and make all members of the helper class public. You have several other options that don’t require using FRIEND_TEST: • Write the tests as members of the fixture class: class Foo { friend class FooTest; … }; class FooTest : public ::testing::Test { protected: … void Test1() {…} // This accesses private members of class Foo. void Test2() {…} // So does this one. }; TEST_F(FooTest, Test1) { Test1(); } TEST_F(FooTest, Test2) { Test2(); } • In the fixture class, write accessors for the tested class’ private members, then use the accessors in your tests: class Foo { friend class FooTest; … }; class FooTest : public ::testing::Test { protected: … T1 get_private_member1(Foo* obj) { return obj->private_member1_; } }; TEST_F(FooTest, Test1) { … get_private_member1(x)

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123