
commit 1749855559044f2547920fa206f72c6fdf7c86f4 Author: Damian Johnson <atagar@torproject.org> Date: Fri Feb 22 08:53:21 2013 -0800 Mocking stdout for tutorial tests Mocking stdout so the tests can be a little more faithful to the tutorial examples. --- test/unit/tutorial.py | 21 +++++++++++++++------ 1 files changed, 15 insertions(+), 6 deletions(-) diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py index 5880f53..21db41b 100644 --- a/test/unit/tutorial.py +++ b/test/unit/tutorial.py @@ -4,14 +4,23 @@ Tests for the examples given in stem's tutorial. from __future__ import with_statement +import StringIO +import sys import unittest from test import mocking class TestTutorial(unittest.TestCase): + stdout, stdout_real = None, None + + def setUp(self): + self.stdout, self.stdout_real = StringIO.StringIO(), sys.stdout + sys.stdout = self.stdout + def tearDown(self): mocking.revert_mocking() + sys.stdout = self.stdout_real def test_the_little_relay_that_could(self): from stem.control import Controller @@ -30,12 +39,12 @@ class TestTutorial(unittest.TestCase): bytes_read = controller.get_info("traffic/read") bytes_written = controller.get_info("traffic/written") - expected_line = "My Tor relay has read 1234 bytes and written 5678." - printed_line = "My Tor relay has read %s bytes and written %s." % (bytes_read, bytes_written) - self.assertEqual(expected_line, printed_line) + print "My Tor relay has read %s bytes and written %s." % (bytes_read, bytes_written) controller.close() + self.assertEqual("My Tor relay has read 1234 bytes and written 5678.\n", self.stdout.getvalue()) + def test_mirror_mirror_on_the_wall(self): from stem.descriptor.server_descriptor import RelayDescriptor from stem.descriptor.reader import DescriptorReader @@ -76,13 +85,13 @@ class TestTutorial(unittest.TestCase): for bw_value in sorted(bw_to_relay.keys(), reverse = True): for nickname in bw_to_relay[bw_value]: - expected_line = "%i. speedyexit (102.13 KB/s)" % count - printed_line = "%i. %s (%s/s)" % (count, nickname, str_tools.get_size_label(bw_value, 2)) - self.assertEqual(expected_line, printed_line) + print "%i. %s (%s/s)" % (count, nickname, str_tools.get_size_label(bw_value, 2)) count += 1 if count > 15: return + expected_stdout = "".join(["%i. speedyexit (102.13 KB/s)\n" % i for i in xrange(1, 4)]) + self.assertEqual(expected_stdout, self.stdout.getvalue()) self.assertEqual(4, count)