def test_stdout_released_after_block(self):
     with collect_stdout() as outer:
         print(u'Outer before')
         with collect_stdout() as inner:
             print(u'Inner')
         print(u'Outer after')
     self.assertEqual(inner.stdout, u'Inner\n')
     self.assertEqual(outer.stdout, u'Outer before\nOuter after\n')
 def test_not_cached_exceptions(self):
     obj = self.Klass()
     with collect_stdout() as output:
         with self.assertRaises(TypeError): obj.method1(4, 7)
         with self.assertRaises(ValueError): obj.method1(7, 4)
         with self.assertRaises(ValueError): obj.method2(7, 4)
     self.assertEqual(output.stdout, u'method1\nmethod1\nmethod2\n')
     with collect_stdout() as output:
         with self.assertRaises(TypeError): obj.method1(4, 7)
         with self.assertRaises(ValueError): obj.method1(7, 4)
         with self.assertRaises(ValueError): obj.method2(7, 4)
     self.assertEqual(output.stdout, u'method1\nmethod1\nmethod2\n')
 def test_methods_are_cached_separately(self):
     obj = self.Klass()
     with collect_stdout() as output:
         self.assertEqual(obj.method1(2, 3), 5)
         self.assertEqual(obj.method2(2, 3), 5)
         self.assertEqual(obj.method3(2, 3), 5)
     self.assertEqual(output.stdout, u'method1\nmethod2\nmethod3\n')
     with collect_stdout() as output:
         self.assertEqual(obj.method1(2, 3), 5)
         self.assertEqual(obj.method2(2, 3), 5)
         self.assertEqual(obj.method3(2, 3), 5)
     self.assertEqual(output.stdout, u'')
 def test_methods_are_cached_per_instance(self):
     obj1 = self.Klass()
     obj2 = self.Klass()
     with collect_stdout() as output:
         self.assertEqual(obj1.method1(2, 3), 5)
         self.assertEqual(obj1.method2(2, 3), 5)
         self.assertEqual(obj1.method3(2, 3), 5)
     self.assertEqual(output.stdout, u'method1\nmethod2\nmethod3\n')
     with collect_stdout() as output:
         self.assertEqual(obj2.method1(2, 3), 5)
         self.assertEqual(obj2.method2(2, 3), 5)
         self.assertEqual(obj2.method3(2, 3), 5)
     self.assertEqual(output.stdout, u'method1\nmethod2\nmethod3\n')
Exemple #5
0
 def _call_dummymail(self, *args, **kwargs):
     mock_process = mock.Mock()
     mock_time = mock.Mock()
     mock_time.sleep.side_effect = KeyboardInterrupt
     with mock.patch.multiple(u'poleno.dummymail.management.commands.dummymail', Process=mock_process, time=mock_time):
         with collect_stdout():
             call_command(u'dummymail', *args, **kwargs)
     return mock_process
 def _call_timewarp(self, *args, **kwargs):
     with collect_stdout() as collected:
         management.call_command(u'timewarp', *args, **kwargs)
     return collected.stdout
 def test_stdout_collected(self):
     with collect_stdout() as collected:
         print(u'Collected text')
     self.assertEqual(collected.stdout, u'Collected text\n')