def test_print_time(self): timer = Timer() # Checks variable date components and constant string output. substrings = [ 'Completed ', 'It took:', 'seconds', '=====', '.', str(datetime.now().year), str(datetime.now().month), str(datetime.now().day) ] saved_stdout = sys.stdout try: out = StringIO() sys.stdout = out timer.print_time() output = out.getvalue() for substring in substrings: self.assertTrue(substring in output, msg=substring) finally: sys.stdout = saved_stdout