title: Testing for stdout in Python Unit Test
body: <p>Hey everyone so for the past week, I've been practicing unit testing with different types of scenarios. I see the importance in Unit testing and want to get as familiar with them as possible. Below is a basic countdown timer. I want to test for the output that is displayed after the While Loop. Here is the code I'm trying to test. </p>
  
  <pre><code>Timer = 11
  
  while Timer &gt; 0:
      Timer = Timer - 1
      print(Timer)
  
  print("Times Up!")
  </code></pre>
  
  <p>Here's my test suite that I used for a simple Hello World program, but it didn't work. I'm assuming because of the outputs i have going on within the while loop. So how would I test for just the "Times Up!" </p>
  
  <pre><code>import unittest
  import sys
  import io
  
  from PopListTestView4 import *
  
  class UnitTest(unittest.TestCase):
  
  
      def test_Output(self):
  
          saved_stdout = sys.stdout
              try:
                  out = io.StringIO()
                  sys.stdout = out
                  output = out.getvalue().strip()
                  assert output == 'Times up!'
              finally:
                  sys.stdout = saved_stdout
  
  
  if __name__ == "__main__":
      suite = unittest.TestLoader().loadTestsFromTestCase(UnitTest)
      unittest.TextTestRunner(verbosity=2).run(suite)
  </code></pre>