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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:05:24+00:00 2026-05-18T05:05:24+00:00

I am studying unit testing. From what I’ve seen, almost all unit tests use

  • 0

I am studying unit testing. From what I’ve seen, almost all unit tests use return value or an output parameter as the expected value in their test case.

What would be the expected value for a function that has no return value or output parameter?

Example:

void unit_test()
{
  cout << "Hello" << endl;
}

Sure, this function is very simple. So, this function does not seem to need to unit test. But this is just a sample. Do you think unit_test function has a side-effect? How would you test it?

  • 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-18T05:05:25+00:00Added an answer on May 18, 2026 at 5:05 am

    If you are writing a function that you know should be tested, then you should design it to be testable in your framework. Here, if your testing is done at a process level where you can verify the process output, then writing to std::cout is fine. Otherwise, you may want to make the output stream a parameter to the function, as in:

    void unit_test(std::ostream& os = std::cout) 
    { 
      os << "Hello" << endl; 
    } 
    

    Then you can test it as in:

    std::ostringstream oss;
    unit_test(oss);
    assert(oss && oss.str() == "Hello");
    

    As this illustrates, making well-tested software requires a bit of give and take… testing requirements feed back into design.

    EDIT: if you must test pre-existing functions without changing them, then consider:

    #include <sstream>
    #include <iostream>
    
    void f()
    {
        std::cout << "hello world\n";
    }
    
    int main()
    {
        std::ostringstream oss;
        std::streambuf* p_cout_streambuf = std::cout.rdbuf();
        std::cout.rdbuf(oss.rdbuf());
    
        f();
    
        std::cout.rdbuf(p_cout_streambuf); // restore
    
        // test your oss content...
        assert(oss && oss.str() == "hello world\n";
        std::cout << oss.str();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Studying compilers course, I am left wondering why use registers at all. It is
After studying TCP/UDP difference all week, I just can't decide which to use. I
I begin studying Unit testing with (NUnit). I know that this type of testing
While studying for a Functional Programming exam, I came across the following questions from
While studying for a Functional Programming exam, I came across the following question from
While studying about JMX, I have seen one of the important feature of it
I'm studying how to use ZeroMQ together with EventMachine. To test things out, I
Studying some code from a codeigniter tut, the following preg_match pattern has me baffled:
I've started TDD some weeks ago. I have to do Unit Tests on a
studying some sample code from an iOS programming course (cs193p fall2010) i came across

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.