##// END OF EJS Templates
Merge pull request #8243 from minrk/test-monkeypatch...
Thomas Kluyver -
r21118:1c4232df merge
parent child Browse files
Show More
@@ -23,6 +23,11 import tempfile
23 import textwrap
23 import textwrap
24 import unittest
24 import unittest
25
25
26 try:
27 from unittest.mock import patch
28 except ImportError:
29 from mock import patch
30
26 import nose.tools as nt
31 import nose.tools as nt
27 from nose import SkipTest
32 from nose import SkipTest
28
33
@@ -448,7 +453,7 class TestMagicRunWithPackage(unittest.TestCase):
448 def with_fake_debugger(func):
453 def with_fake_debugger(func):
449 @functools.wraps(func)
454 @functools.wraps(func)
450 def wrapper(*args, **kwds):
455 def wrapper(*args, **kwds):
451 with tt.monkeypatch(debugger.Pdb, 'run', staticmethod(eval)):
456 with patch.object(debugger.Pdb, 'run', staticmethod(eval)):
452 return func(*args, **kwds)
457 return func(*args, **kwds)
453 return wrapper
458 return wrapper
454
459
@@ -5,10 +5,12
5
5
6 from __future__ import print_function
6 from __future__ import print_function
7
7
8 from collections import OrderedDict
8 try:
9 from unittest.mock import patch
10 except ImportError:
11 from mock import patch
9
12
10 import nose.tools as nt
13 import nose.tools as nt
11 import IPython.testing.tools as tt
12
14
13 from IPython.kernel.comm import Comm
15 from IPython.kernel.comm import Comm
14 from IPython.html import widgets
16 from IPython.html import widgets
@@ -354,7 +356,7 def test_priority():
354
356
355 @nt.with_setup(clear_display)
357 @nt.with_setup(clear_display)
356 def test_decorator_kwarg():
358 def test_decorator_kwarg():
357 with tt.monkeypatch(interaction, 'display', record_display):
359 with patch.object(interaction, 'display', record_display):
358 @interact(a=5)
360 @interact(a=5)
359 def foo(a):
361 def foo(a):
360 pass
362 pass
@@ -373,7 +375,7 def test_interact_instancemethod():
373
375
374 f = Foo()
376 f = Foo()
375
377
376 with tt.monkeypatch(interaction, 'display', record_display):
378 with patch.object(interaction, 'display', record_display):
377 g = interact(f.show, x=(1,10))
379 g = interact(f.show, x=(1,10))
378 nt.assert_equal(len(displayed), 1)
380 nt.assert_equal(len(displayed), 1)
379 w = displayed[0].children[0]
381 w = displayed[0].children[0]
@@ -384,7 +386,7 def test_interact_instancemethod():
384
386
385 @nt.with_setup(clear_display)
387 @nt.with_setup(clear_display)
386 def test_decorator_no_call():
388 def test_decorator_no_call():
387 with tt.monkeypatch(interaction, 'display', record_display):
389 with patch.object(interaction, 'display', record_display):
388 @interact
390 @interact
389 def foo(a='default'):
391 def foo(a='default'):
390 pass
392 pass
@@ -399,7 +401,7 def test_decorator_no_call():
399 def test_call_interact():
401 def test_call_interact():
400 def foo(a='default'):
402 def foo(a='default'):
401 pass
403 pass
402 with tt.monkeypatch(interaction, 'display', record_display):
404 with patch.object(interaction, 'display', record_display):
403 ifoo = interact(foo)
405 ifoo = interact(foo)
404 nt.assert_equal(len(displayed), 1)
406 nt.assert_equal(len(displayed), 1)
405 w = displayed[0].children[0]
407 w = displayed[0].children[0]
@@ -412,7 +414,7 def test_call_interact():
412 def test_call_interact_kwargs():
414 def test_call_interact_kwargs():
413 def foo(a='default'):
415 def foo(a='default'):
414 pass
416 pass
415 with tt.monkeypatch(interaction, 'display', record_display):
417 with patch.object(interaction, 'display', record_display):
416 ifoo = interact(foo, a=10)
418 ifoo = interact(foo, a=10)
417 nt.assert_equal(len(displayed), 1)
419 nt.assert_equal(len(displayed), 1)
418 w = displayed[0].children[0]
420 w = displayed[0].children[0]
@@ -425,7 +427,7 def test_call_interact_kwargs():
425 def test_call_decorated_on_trait_change():
427 def test_call_decorated_on_trait_change():
426 """test calling @interact decorated functions"""
428 """test calling @interact decorated functions"""
427 d = {}
429 d = {}
428 with tt.monkeypatch(interaction, 'display', record_display):
430 with patch.object(interaction, 'display', record_display):
429 @interact
431 @interact
430 def foo(a='default'):
432 def foo(a='default'):
431 d['a'] = a
433 d['a'] = a
@@ -449,7 +451,7 def test_call_decorated_on_trait_change():
449 def test_call_decorated_kwargs_on_trait_change():
451 def test_call_decorated_kwargs_on_trait_change():
450 """test calling @interact(foo=bar) decorated functions"""
452 """test calling @interact(foo=bar) decorated functions"""
451 d = {}
453 d = {}
452 with tt.monkeypatch(interaction, 'display', record_display):
454 with patch.object(interaction, 'display', record_display):
453 @interact(a='kwarg')
455 @interact(a='kwarg')
454 def foo(a='default'):
456 def foo(a='default'):
455 d['a'] = a
457 d['a'] = a
@@ -1,18 +1,18
1 # encoding: utf-8
1 # encoding: utf-8
2 """Tests for IPython.utils.path.py"""
2 """Tests for IPython.utils.path.py"""
3
3
4 #-----------------------------------------------------------------------------
4 # Copyright (c) IPython Development Team.
5 # Copyright (C) 2008-2011 The IPython Development Team
5 # Distributed under the terms of the Modified BSD License.
6 #
6
7 # Distributed under the terms of the BSD License. The full license is in
7 try:
8 # the file COPYING, distributed as part of this software.
8 from unittest.mock import patch
9 #-----------------------------------------------------------------------------
9 except ImportError:
10 from mock import patch
10
11
11 import nose.tools as nt
12 import nose.tools as nt
12
13
13 from IPython.lib import latextools
14 from IPython.lib import latextools
14 from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib
15 from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib
15 from IPython.testing.tools import monkeypatch
16 from IPython.utils.process import FindCmdError
16 from IPython.utils.process import FindCmdError
17
17
18
18
@@ -29,7 +29,7 def check_latex_to_png_dvipng_fails_when_no_cmd(command):
29 if arg == command:
29 if arg == command:
30 raise FindCmdError
30 raise FindCmdError
31
31
32 with monkeypatch(latextools, "find_cmd", mock_find_cmd):
32 with patch.object(latextools, "find_cmd", mock_find_cmd):
33 nt.assert_equals(latextools.latex_to_png_dvipng("whatever", True),
33 nt.assert_equals(latextools.latex_to_png_dvipng("whatever", True),
34 None)
34 None)
35
35
@@ -46,7 +46,7 def test_latex_to_png_dvipng_runs():
46 for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]:
46 for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]:
47 yield (latextools.latex_to_png_dvipng, s, wrap)
47 yield (latextools.latex_to_png_dvipng, s, wrap)
48
48
49 with monkeypatch(latextools, "kpsewhich", mock_kpsewhich):
49 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
50 yield (latextools.latex_to_png_dvipng, s, wrap)
50 yield (latextools.latex_to_png_dvipng, s, wrap)
51
51
52 @skipif_not_matplotlib
52 @skipif_not_matplotlib
@@ -61,7 +61,7 def test_latex_to_png_mpl_runs():
61 for (s, wrap) in [("$x^2$", False), ("x^2", True)]:
61 for (s, wrap) in [("$x^2$", False), ("x^2", True)]:
62 yield (latextools.latex_to_png_mpl, s, wrap)
62 yield (latextools.latex_to_png_mpl, s, wrap)
63
63
64 with monkeypatch(latextools, "kpsewhich", mock_kpsewhich):
64 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
65 yield (latextools.latex_to_png_mpl, s, wrap)
65 yield (latextools.latex_to_png_mpl, s, wrap)
66
66
67 @skipif_not_matplotlib
67 @skipif_not_matplotlib
@@ -78,7 +78,7 def test_genelatex_no_wrap():
78 assert False, ("kpsewhich should not be called "
78 assert False, ("kpsewhich should not be called "
79 "(called with {0})".format(filename))
79 "(called with {0})".format(filename))
80
80
81 with monkeypatch(latextools, "kpsewhich", mock_kpsewhich):
81 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
82 nt.assert_equals(
82 nt.assert_equals(
83 '\n'.join(latextools.genelatex("body text", False)),
83 '\n'.join(latextools.genelatex("body text", False)),
84 r'''\documentclass{article}
84 r'''\documentclass{article}
@@ -100,7 +100,7 def test_genelatex_wrap_with_breqn():
100 nt.assert_equals(filename, "breqn.sty")
100 nt.assert_equals(filename, "breqn.sty")
101 return "path/to/breqn.sty"
101 return "path/to/breqn.sty"
102
102
103 with monkeypatch(latextools, "kpsewhich", mock_kpsewhich):
103 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
104 nt.assert_equals(
104 nt.assert_equals(
105 '\n'.join(latextools.genelatex("x^2", True)),
105 '\n'.join(latextools.genelatex("x^2", True)),
106 r'''\documentclass{article}
106 r'''\documentclass{article}
@@ -125,7 +125,7 def test_genelatex_wrap_without_breqn():
125 nt.assert_equals(filename, "breqn.sty")
125 nt.assert_equals(filename, "breqn.sty")
126 return None
126 return None
127
127
128 with monkeypatch(latextools, "kpsewhich", mock_kpsewhich):
128 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
129 nt.assert_equals(
129 nt.assert_equals(
130 '\n'.join(latextools.genelatex("x^2", True)),
130 '\n'.join(latextools.genelatex("x^2", True)),
131 r'''\documentclass{article}
131 r'''\documentclass{article}
@@ -444,17 +444,6 def make_tempfile(name):
444 os.unlink(name)
444 os.unlink(name)
445
445
446
446
447 @contextmanager
448 def monkeypatch(obj, name, attr):
449 """
450 Context manager to replace attribute named `name` in `obj` with `attr`.
451 """
452 orig = getattr(obj, name)
453 setattr(obj, name, attr)
454 yield
455 setattr(obj, name, orig)
456
457
458 def help_output_test(subcommand=''):
447 def help_output_test(subcommand=''):
459 """test that `ipython [subcommand] -h` works"""
448 """test that `ipython [subcommand] -h` works"""
460 cmd = get_ipython_cmd() + [subcommand, '-h']
449 cmd = get_ipython_cmd() + [subcommand, '-h']
@@ -1,15 +1,16
1 #-----------------------------------------------------------------------------
1 # Copyright (c) IPython Development Team.
2 # Copyright (C) 2012 The IPython Development Team
2 # Distributed under the terms of the Modified BSD License.
3 #
4 # Distributed under the terms of the BSD License. The full license is in
5 # the file COPYING, distributed as part of this software.
6 #-----------------------------------------------------------------------------
7
3
8 import os
4 import os
9 import sys
5 import sys
10 import unittest
6 import unittest
11 import base64
7 import base64
12
8
9 try:
10 from unittest.mock import patch
11 except ImportError:
12 from mock import patch
13
13 from IPython.kernel import KernelClient
14 from IPython.kernel import KernelClient
14 from IPython.terminal.console.interactiveshell import ZMQTerminalInteractiveShell
15 from IPython.terminal.console.interactiveshell import ZMQTerminalInteractiveShell
15 from IPython.utils.tempdir import TemporaryDirectory
16 from IPython.utils.tempdir import TemporaryDirectory
@@ -54,7 +55,7 class ZMQTerminalInteractiveShellTestCase(unittest.TestCase):
54 open_called_with.append(arg)
55 open_called_with.append(arg)
55 return Struct(show=lambda: show_called_with.append(None))
56 return Struct(show=lambda: show_called_with.append(None))
56
57
57 with monkeypatch(PIL.Image, 'open', fake_open):
58 with patch.object(PIL.Image, 'open', fake_open):
58 self.shell.handle_image_PIL(self.data, self.mime)
59 self.shell.handle_image_PIL(self.data, self.mime)
59
60
60 self.assertEqual(len(open_called_with), 1)
61 self.assertEqual(len(open_called_with), 1)
General Comments 0
You need to be logged in to leave comments. Login now