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

  • Home
  • SEARCH
  • 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 4325336
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:15:11+00:00 2026-05-21T09:15:11+00:00

Let us assume I have a to be tested class that has the following

  • 0

Let us assume I have a to be tested class that has the following method:

void
MyClass::sayHello()
{
   std::cout << "Hello";
}

Now, within my google test I would like to verify that this output has been made. What would be the lastConsoleOutput equivalent be as used in my pseudo code below?

// Tests if sayHello() outputs Hello
TEST(MyClassTest, sayHello)\
{
  EXPECT_EQ(lastConsoleOutput,"Hello");
}

Thank you for any feedback!

  • 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-05-21T09:15:12+00:00Added an answer on May 21, 2026 at 9:15 am

    In this case I would avoid redirecting or testing for values in stdout or stderr, since the access to those streams is not threads-safe in a way that output buffer may not be appended and flushed as possibly predicted.

    From a testing perspective I would suggest refactoring the method to be stateless and keep the state (a.k.a. std::cout) somewhere else. In your example you start testing behavior of an external API and not the actual modification in your object.

    class MyClass {
    
        std::sstream state;
    
    public:
    
        void print(){ std::cout << state.str(); } // no need to test this method, only external API
    
        void changeState() {
            state << "Hello" << std::endl; // this should be tested 
        }
    
    }
    

    In your testing code you can now easily perform the test using

    // Tests if sayHello() outputs Hello
    TEST(MyClassTest, sayHello)
    {
      myClass.changeState();
      EXPECT_STREQ(myClass.state.str().c_str(),"Hello");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume I have the following code: public class MainClass { public static void
Let's assume we have a template function foo: template<class T> void foo(T arg) {
Let's assume we have the following method in the business layer. What's the best
Let's assume we have the following Map index: public class CommentsIndex : AbstractIndexCreationTask<Post> {
Let's assume I have a c++ class that have properly implemented a copy constructor
Let's assume we have a class Student with the following constructor: /** Initializes a
Let's assume I have defined the following Entity: @Entity class User{ @Id @GeneratedValue @Column(name
Let's assume I have a file named Main.java with the following code: public class
Let's assume I have the following domain: public class Movie { public string Id
Let's assume we have the following class : public class ImageButton extends MovieClip {

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.