##// END OF EJS Templates
Use AssertPrints in tests for autoreload extension.
Thomas Kluyver -
r4904:12c51795
parent child
Show More
@@ -7,6 +7,7 import time
7 from StringIO import StringIO
7 from StringIO import StringIO
8
8
9 import nose.tools as nt
9 import nose.tools as nt
10 import IPython.testing.tools as tt
10
11
11 from IPython.extensions.autoreload import AutoreloadInterface
12 from IPython.extensions.autoreload import AutoreloadInterface
12 from IPython.core.hooks import TryNext
13 from IPython.core.hooks import TryNext
@@ -197,19 +198,11 class Bar: # old-style class: weakref doesn't work for it on Python < 2.7
197 a syntax error
198 a syntax error
198 """)
199 """)
199
200
200 old_stderr = sys.stderr
201 with tt.AssertPrints(('[autoreload of %s failed:' % mod_name), channel='stderr'):
201 new_stderr = StringIO()
202 sys.stderr = new_stderr
203 try:
204 self.shell.run_code("pass") # trigger reload
202 self.shell.run_code("pass") # trigger reload
203 with tt.AssertNotPrints(('[autoreload of %s failed:' % mod_name), channel='stderr'):
205 self.shell.run_code("pass") # trigger another reload
204 self.shell.run_code("pass") # trigger another reload
206 check_module_contents()
205 check_module_contents()
207 finally:
208 sys.stderr = old_stderr
209
210 nt.assert_true(('[autoreload of %s failed:' % mod_name) in
211 new_stderr.getvalue())
212 nt.assert_equal(new_stderr.getvalue().count('[autoreload of'), 1)
213
206
214 #
207 #
215 # Rewrite module (this time reload should succeed)
208 # Rewrite module (this time reload should succeed)
@@ -342,22 +342,23 class AssertPrints(object):
342
342
343 Examples
343 Examples
344 --------
344 --------
345 >>> with AssertPrints("abc"):
345 >>> with AssertPrints("abc", suppress=False):
346 ... print "abcd"
346 ... print "abcd"
347 ... print "def"
347 ... print "def"
348 ...
348 ...
349 abcd
349 abcd
350 def
350 def
351 """
351 """
352 def __init__(self, s, channel='stdout'):
352 def __init__(self, s, channel='stdout', suppress=True):
353 self.s = s
353 self.s = s
354 self.channel = channel
354 self.channel = channel
355 self.suppress = suppress
355
356
356 def __enter__(self):
357 def __enter__(self):
357 self.orig_stream = getattr(sys, self.channel)
358 self.orig_stream = getattr(sys, self.channel)
358 self.buffer = MyStringIO()
359 self.buffer = MyStringIO()
359 self.tee = Tee(self.buffer, channel=self.channel)
360 self.tee = Tee(self.buffer, channel=self.channel)
360 setattr(sys, self.channel, self.tee)
361 setattr(sys, self.channel, self.buffer if self.suppress else self.tee)
361
362
362 def __exit__(self, etype, value, traceback):
363 def __exit__(self, etype, value, traceback):
363 self.tee.flush()
364 self.tee.flush()
@@ -366,6 +367,17 class AssertPrints(object):
366 assert self.s in printed, notprinted_msg.format(self.s, self.channel, printed)
367 assert self.s in printed, notprinted_msg.format(self.s, self.channel, printed)
367 return False
368 return False
368
369
370 class AssertNotPrints(AssertPrints):
371 """Context manager for checking that certain output *isn't* produced.
372
373 Counterpart of AssertPrints"""
374 def __exit__(self, etype, value, traceback):
375 self.tee.flush()
376 setattr(sys, self.channel, self.orig_stream)
377 printed = self.buffer.getvalue()
378 assert self.s not in printed, notprinted_msg.format(self.s, self.channel, printed)
379 return False
380
369 @contextmanager
381 @contextmanager
370 def mute_warn():
382 def mute_warn():
371 from IPython.utils import warn
383 from IPython.utils import warn
General Comments 0
You need to be logged in to leave comments. Login now