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 9225719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:39:07+00:00 2026-06-18T04:39:07+00:00

I thought of writing ut for a complex algorithm, which consits of many steps.

  • 0

I thought of writing ut for a complex algorithm, which consits of many steps. For example, the algorithm class like below. And I want to test every step. But it’s obvious that i can process the private function.
I am using the google gtest.

How shall I design the UT?

Class Algorithm
{private:
    PreProcess();
    Process();
    PostProcess();
}
  • 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-18T04:39:08+00:00Added an answer on June 18, 2026 at 4:39 am

    There is a specific section in the Google Test documentation dealing with testing private code.

    Short version is, try to avoid testing non-public functions. If you have to, then you can either change your class design, or your test can be made a friend of the class.

    GTest provides a helper macro for this last option: FRIEND_TEST, but beware of namespace issues with this. Your test needs to be defined in the same namespace as the class being tested for this macro to work.

    So something like:

    #include <iostream>
    #include "gtest/gtest.h"
    
    class Algorithm {
     private:
      bool PreProcess() { std::cout << "Pre\n"; return true; }
      bool Process() { std::cout << "Process\n"; return true; }
      bool PostProcess() { std::cout << "Post\n"; return true; }
      FRIEND_TEST(AlgorithmPrivateTest, PreProcess);
      FRIEND_TEST(AlgorithmPrivateTest, Process);
      FRIEND_TEST(AlgorithmPrivateTest, PostProcess);
    };
    
    class AlgorithmPrivateTest : public testing::Test {
     protected:
      AlgorithmPrivateTest() : algorithm_() {}
      Algorithm algorithm_;
    };
    
    TEST_F(AlgorithmPrivateTest, PreProcess) {
      EXPECT_TRUE(algorithm_.PreProcess());
    }
    
    TEST_F(AlgorithmPrivateTest, Process) {
      EXPECT_TRUE(algorithm_.Process());
    }
    
    TEST_F(AlgorithmPrivateTest, PostProcess) {
      EXPECT_TRUE(algorithm_.PostProcess());
    }
    
    int main(int argc, char **argv) {
      testing::InitGoogleTest(&argc, argv);
      return RUN_ALL_TESTS();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an XSD for a rather complex XML file format. At many points,
I want to display complex numbers in trig form. For example: z = (-4)^(1/4);
I am writing a string parser and the thought occurred to me that there
As I was writing a for loop earlier today, I thought that there must
Wondering when a write-back should be performed? I thought it was when you're writing
I'm writing an application in C# where I want to maintain a large number
Sometimes when i'm writing moderately complex SELECT statements with a few JOINs, wrong key
I'm writing a fairly complex stored procedure to search an image library. I was
I have a more complex webapp in JSF2 (+EJB3.1 +JPA2 on glassfish 3.1), which
We have a system which has complex user permissions which determine which contacts a

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.