##// END OF EJS Templates
Merge pull request #12666 from Carreau/pytest-yields
Matthias Bussonnier -
r26194:69589881 merge
parent child Browse files
Show More
@@ -41,7 +41,7 b' install:'
41 fi
41 fi
42 - pip install -e file://$PWD#egg=ipython[test] --upgrade
42 - pip install -e file://$PWD#egg=ipython[test] --upgrade
43 - pip install trio curio --upgrade --upgrade-strategy eager
43 - pip install trio curio --upgrade --upgrade-strategy eager
44 - pip install 'pytest<6' 'matplotlib !=3.2.0'
44 - pip install 'pytest' 'matplotlib !=3.2.0'
45 - pip install codecov check-manifest pytest-cov --upgrade
45 - pip install codecov check-manifest pytest-cov --upgrade
46
46
47
47
@@ -138,7 +138,7 b" def latex_to_png_dvipng(s, wrap, color='Black', scale=1.0):"
138 except FindCmdError:
138 except FindCmdError:
139 return None
139 return None
140 try:
140 try:
141 workdir = PurePath(tempfile.mkdtemp())
141 workdir = Path(tempfile.mkdtemp())
142 tmpfile = workdir.joinpath("tmp.tex")
142 tmpfile = workdir.joinpath("tmp.tex")
143 dvifile = workdir.joinpath("tmp.dvi")
143 dvifile = workdir.joinpath("tmp.dvi")
144 outfile = workdir.joinpath("tmp.png")
144 outfile = workdir.joinpath("tmp.png")
@@ -9,11 +9,16 b' import nose.tools as nt'
9 import pytest
9 import pytest
10
10
11 from IPython.lib import latextools
11 from IPython.lib import latextools
12 from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib
12 from IPython.testing.decorators import (
13 onlyif_cmds_exist,
14 skipif_not_matplotlib,
15 skip_iptest_but_not_pytest,
16 )
13 from IPython.utils.process import FindCmdError
17 from IPython.utils.process import FindCmdError
14
18
15
19
16 @pytest.mark.parametrize('command', ['latex', 'dvipng'])
20 @pytest.mark.parametrize('command', ['latex', 'dvipng'])
21 @skip_iptest_but_not_pytest
17 def test_check_latex_to_png_dvipng_fails_when_no_cmd(command):
22 def test_check_latex_to_png_dvipng_fails_when_no_cmd(command):
18 def mock_find_cmd(arg):
23 def mock_find_cmd(arg):
19 if arg == command:
24 if arg == command:
@@ -23,8 +28,15 b' def test_check_latex_to_png_dvipng_fails_when_no_cmd(command):'
23 assert latextools.latex_to_png_dvipng("whatever", True) == None
28 assert latextools.latex_to_png_dvipng("whatever", True) == None
24
29
25
30
26 @onlyif_cmds_exist('latex', 'dvipng')
31 @contextmanager
27 def test_latex_to_png_dvipng_runs():
32 def no_op(*args, **kwargs):
33 yield
34
35
36 @skip_iptest_but_not_pytest
37 @onlyif_cmds_exist("latex", "dvipng")
38 @pytest.mark.parametrize("s, wrap", [(u"$$x^2$$", False), (u"x^2", True)])
39 def test_latex_to_png_dvipng_runs(s, wrap):
28 """
40 """
29 Test that latex_to_png_dvipng just runs without error.
41 Test that latex_to_png_dvipng just runs without error.
30 """
42 """
@@ -32,36 +44,32 b' def test_latex_to_png_dvipng_runs():'
32 assert filename == "breqn.sty"
44 assert filename == "breqn.sty"
33 return None
45 return None
34
46
35 for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]:
47 latextools.latex_to_png_dvipng(s, wrap)
36 yield (latextools.latex_to_png_dvipng, s, wrap)
37
48
38 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
49 with patch_latextool(mock_kpsewhich):
39 yield (latextools.latex_to_png_dvipng, s, wrap)
50 latextools.latex_to_png_dvipng(s, wrap)
40
51
41
52
42 @contextmanager
43 def no_op(*args, **kwargs):
44 yield
45
46 def mock_kpsewhich(filename):
53 def mock_kpsewhich(filename):
47 assert filename == "breqn.sty"
54 assert filename == "breqn.sty"
48 return None
55 return None
49
56
50 @contextmanager
57 @contextmanager
51 def patch_latextool():
58 def patch_latextool(mock=mock_kpsewhich):
52 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
59 with patch.object(latextools, "kpsewhich", mock):
53 yield
60 yield
54
61
55 @pytest.mark.parametrize('context', [no_op, patch_latextool])
62 @pytest.mark.parametrize('context', [no_op, patch_latextool])
56 @pytest.mark.parametrize('s_wrap', [("$x^2$", False), ("x^2", True)])
63 @pytest.mark.parametrize('s_wrap', [("$x^2$", False), ("x^2", True)])
64 @skip_iptest_but_not_pytest
57 def test_latex_to_png_mpl_runs(s_wrap, context):
65 def test_latex_to_png_mpl_runs(s_wrap, context):
58 """
66 """
59 Test that latex_to_png_mpl just runs without error.
67 Test that latex_to_png_mpl just runs without error.
60 """
68 """
61 try:
69 try:
62 import matplotbli
70 import matplotlib
63 except ImportError:
71 except ImportError:
64 pytest.skip("This needs matplotlib to be availlable")
72 pytest.skip("This needs matplotlib to be available")
65 return
73 return
66 s, wrap = s_wrap
74 s, wrap = s_wrap
67 with context():
75 with context():
@@ -81,7 +89,7 b' def test_genelatex_no_wrap():'
81 assert False, ("kpsewhich should not be called "
89 assert False, ("kpsewhich should not be called "
82 "(called with {0})".format(filename))
90 "(called with {0})".format(filename))
83
91
84 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
92 with patch_latextool(mock_kpsewhich):
85 assert '\n'.join(latextools.genelatex("body text", False)) == r'''\documentclass{article}
93 assert '\n'.join(latextools.genelatex("body text", False)) == r'''\documentclass{article}
86 \usepackage{amsmath}
94 \usepackage{amsmath}
87 \usepackage{amsthm}
95 \usepackage{amsthm}
@@ -101,7 +109,7 b' def test_genelatex_wrap_with_breqn():'
101 assert filename == "breqn.sty"
109 assert filename == "breqn.sty"
102 return "path/to/breqn.sty"
110 return "path/to/breqn.sty"
103
111
104 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
112 with patch_latextool(mock_kpsewhich):
105 assert '\n'.join(latextools.genelatex("x^2", True)) == r'''\documentclass{article}
113 assert '\n'.join(latextools.genelatex("x^2", True)) == r'''\documentclass{article}
106 \usepackage{amsmath}
114 \usepackage{amsmath}
107 \usepackage{amsthm}
115 \usepackage{amsthm}
@@ -124,7 +132,7 b' def test_genelatex_wrap_without_breqn():'
124 assert filename == "breqn.sty"
132 assert filename == "breqn.sty"
125 return None
133 return None
126
134
127 with patch.object(latextools, "kpsewhich", mock_kpsewhich):
135 with patch_latextool(mock_kpsewhich):
128 assert '\n'.join(latextools.genelatex("x^2", True)) == r'''\documentclass{article}
136 assert '\n'.join(latextools.genelatex("x^2", True)) == r'''\documentclass{article}
129 \usepackage{amsmath}
137 \usepackage{amsmath}
130 \usepackage{amsthm}
138 \usepackage{amsthm}
@@ -12,9 +12,10 b' import string'
12 import unittest
12 import unittest
13
13
14 import nose.tools as nt
14 import nose.tools as nt
15 import pytest
15
16
16 from IPython.lib import pretty
17 from IPython.lib import pretty
17 from IPython.testing.decorators import skip_without
18 from IPython.testing.decorators import skip_without, skip_iptest_but_not_pytest
18
19
19 from io import StringIO
20 from io import StringIO
20
21
@@ -105,17 +106,36 b' def test_callability_checking():'
105 nt.assert_equal(gotoutput, expectedoutput)
106 nt.assert_equal(gotoutput, expectedoutput)
106
107
107
108
108 def test_sets():
109 @pytest.mark.parametrize(
110 "obj,expected_output",
111 zip(
112 [
113 set(),
114 frozenset(),
115 set([1]),
116 frozenset([1]),
117 set([1, 2]),
118 frozenset([1, 2]),
119 set([-1, -2, -3]),
120 ],
121 [
122 "set()",
123 "frozenset()",
124 "{1}",
125 "frozenset({1})",
126 "{1, 2}",
127 "frozenset({1, 2})",
128 "{-3, -2, -1}",
129 ],
130 ),
131 )
132 @skip_iptest_but_not_pytest
133 def test_sets(obj, expected_output):
109 """
134 """
110 Test that set and frozenset use Python 3 formatting.
135 Test that set and frozenset use Python 3 formatting.
111 """
136 """
112 objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]),
137 got_output = pretty.pretty(obj)
113 frozenset([1, 2]), set([-1, -2, -3])]
138 nt.assert_equal(got_output, expected_output)
114 expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}',
115 'frozenset({1, 2})', '{-3, -2, -1}']
116 for obj, expected_output in zip(objects, expected):
117 got_output = pretty.pretty(obj)
118 yield nt.assert_equal, got_output, expected_output
119
139
120
140
121 @skip_without('xxlimited')
141 @skip_without('xxlimited')
@@ -154,6 +154,17 b' def make_label_dec(label, ds=None):'
154 return decor
154 return decor
155
155
156
156
157 def skip_iptest_but_not_pytest(f):
158 """
159 Warnign this will make the test invisible to iptest.
160 """
161 import os
162
163 if os.environ.get("IPTEST_WORKING_DIR", None) is not None:
164 f.__test__ = False
165 return f
166
167
157 # Inspired by numpy's skipif, but uses the full apply_wrapper utility to
168 # Inspired by numpy's skipif, but uses the full apply_wrapper utility to
158 # preserve function metadata better and allows the skip condition to be a
169 # preserve function metadata better and allows the skip condition to be a
159 # callable.
170 # callable.
@@ -365,9 +376,14 b' def onlyif_cmds_exist(*commands):'
365 Decorator to skip test when at least one of `commands` is not found.
376 Decorator to skip test when at least one of `commands` is not found.
366 """
377 """
367 for cmd in commands:
378 for cmd in commands:
379 reason = "This test runs only if command '{cmd}' is installed"
368 if not shutil.which(cmd):
380 if not shutil.which(cmd):
369 return skip("This test runs only if command '{0}' "
381 if os.environ.get("IPTEST_WORKING_DIR", None) is not None:
370 "is installed".format(cmd))
382 return skip(reason)
383 else:
384 import pytest
385
386 return pytest.mark.skip(reason=reason)
371 return null_deco
387 return null_deco
372
388
373 def onlyif_any_cmd_exists(*commands):
389 def onlyif_any_cmd_exists(*commands):
@@ -16,6 +16,9 b''
16 import sys
16 import sys
17
17
18 import nose.tools as nt
18 import nose.tools as nt
19 import pytest
20
21 from IPython.testing.decorators import skip_iptest_but_not_pytest
19
22
20 from IPython.utils import capture
23 from IPython.utils import capture
21
24
@@ -66,38 +69,45 b' hello_stderr = "hello, stderr"'
66 #-----------------------------------------------------------------------------
69 #-----------------------------------------------------------------------------
67 # Test Functions
70 # Test Functions
68 #-----------------------------------------------------------------------------
71 #-----------------------------------------------------------------------------
69
72 @pytest.mark.parametrize("method_mime", _mime_map.items())
70 def test_rich_output_empty():
73 @skip_iptest_but_not_pytest
74 def test_rich_output_empty(method_mime):
71 """RichOutput with no args"""
75 """RichOutput with no args"""
72 rich = capture.RichOutput()
76 rich = capture.RichOutput()
73 for method, mime in _mime_map.items():
77 method, mime = method_mime
74 yield nt.assert_equal, getattr(rich, method)(), None
78 nt.assert_equal(getattr(rich, method)(), None)
75
79
76 def test_rich_output():
80 def test_rich_output():
77 """test RichOutput basics"""
81 """test RichOutput basics"""
78 data = basic_data
82 data = basic_data
79 metadata = basic_metadata
83 metadata = basic_metadata
80 rich = capture.RichOutput(data=data, metadata=metadata)
84 rich = capture.RichOutput(data=data, metadata=metadata)
81 yield nt.assert_equal, rich._repr_html_(), data['text/html']
85 nt.assert_equal(rich._repr_html_(), data["text/html"])
82 yield nt.assert_equal, rich._repr_png_(), (data['image/png'], metadata['image/png'])
86 nt.assert_equal(rich._repr_png_(), (data["image/png"], metadata["image/png"]))
83 yield nt.assert_equal, rich._repr_latex_(), None
87 nt.assert_equal(rich._repr_latex_(), None)
84 yield nt.assert_equal, rich._repr_javascript_(), None
88 nt.assert_equal(rich._repr_javascript_(), None)
85 yield nt.assert_equal, rich._repr_svg_(), None
89 nt.assert_equal(rich._repr_svg_(), None)
86
90
87 def test_rich_output_no_metadata():
91
92 @skip_iptest_but_not_pytest
93 @pytest.mark.parametrize("method_mime", _mime_map.items())
94 def test_rich_output_no_metadata(method_mime):
88 """test RichOutput with no metadata"""
95 """test RichOutput with no metadata"""
89 data = full_data
96 data = full_data
90 rich = capture.RichOutput(data=data)
97 rich = capture.RichOutput(data=data)
91 for method, mime in _mime_map.items():
98 method, mime = method_mime
92 yield nt.assert_equal, getattr(rich, method)(), data[mime]
99 nt.assert_equal(getattr(rich, method)(), data[mime])
100
93
101
94 def test_rich_output_metadata():
102 @skip_iptest_but_not_pytest
103 @pytest.mark.parametrize("method_mime", _mime_map.items())
104 def test_rich_output_metadata(method_mime):
95 """test RichOutput with metadata"""
105 """test RichOutput with metadata"""
96 data = full_data
106 data = full_data
97 metadata = full_metadata
107 metadata = full_metadata
98 rich = capture.RichOutput(data=data, metadata=metadata)
108 rich = capture.RichOutput(data=data, metadata=metadata)
99 for method, mime in _mime_map.items():
109 method, mime = method_mime
100 yield nt.assert_equal, getattr(rich, method)(), (data[mime], metadata[mime])
110 nt.assert_equal(getattr(rich, method)(), (data[mime], metadata[mime]))
101
111
102 def test_rich_output_display():
112 def test_rich_output_display():
103 """test RichOutput.display
113 """test RichOutput.display
@@ -109,10 +119,10 b' def test_rich_output_display():'
109 rich = capture.RichOutput(data=data)
119 rich = capture.RichOutput(data=data)
110 with capture.capture_output() as cap:
120 with capture.capture_output() as cap:
111 rich.display()
121 rich.display()
112 yield nt.assert_equal, len(cap.outputs), 1
122 nt.assert_equal(len(cap.outputs), 1)
113 rich2 = cap.outputs[0]
123 rich2 = cap.outputs[0]
114 yield nt.assert_equal, rich2.data, rich.data
124 nt.assert_equal(rich2.data, rich.data)
115 yield nt.assert_equal, rich2.metadata, rich.metadata
125 nt.assert_equal(rich2.metadata, rich.metadata)
116
126
117 def test_capture_output():
127 def test_capture_output():
118 """capture_output works"""
128 """capture_output works"""
@@ -121,8 +131,9 b' def test_capture_output():'
121 print(hello_stdout, end="")
131 print(hello_stdout, end="")
122 print(hello_stderr, end="", file=sys.stderr)
132 print(hello_stderr, end="", file=sys.stderr)
123 rich.display()
133 rich.display()
124 yield nt.assert_equal, hello_stdout, cap.stdout
134 nt.assert_equal(hello_stdout, cap.stdout)
125 yield nt.assert_equal, hello_stderr, cap.stderr
135 nt.assert_equal(hello_stderr, cap.stderr)
136
126
137
127 def test_capture_output_no_stdout():
138 def test_capture_output_no_stdout():
128 """test capture_output(stdout=False)"""
139 """test capture_output(stdout=False)"""
@@ -131,9 +142,10 b' def test_capture_output_no_stdout():'
131 print(hello_stdout, end="")
142 print(hello_stdout, end="")
132 print(hello_stderr, end="", file=sys.stderr)
143 print(hello_stderr, end="", file=sys.stderr)
133 rich.display()
144 rich.display()
134 yield nt.assert_equal, "", cap.stdout
145 nt.assert_equal("", cap.stdout)
135 yield nt.assert_equal, hello_stderr, cap.stderr
146 nt.assert_equal(hello_stderr, cap.stderr)
136 yield nt.assert_equal, len(cap.outputs), 1
147 nt.assert_equal(len(cap.outputs), 1)
148
137
149
138 def test_capture_output_no_stderr():
150 def test_capture_output_no_stderr():
139 """test capture_output(stderr=False)"""
151 """test capture_output(stderr=False)"""
@@ -143,9 +155,10 b' def test_capture_output_no_stderr():'
143 print(hello_stdout, end="")
155 print(hello_stdout, end="")
144 print(hello_stderr, end="", file=sys.stderr)
156 print(hello_stderr, end="", file=sys.stderr)
145 rich.display()
157 rich.display()
146 yield nt.assert_equal, hello_stdout, cap.stdout
158 nt.assert_equal(hello_stdout, cap.stdout)
147 yield nt.assert_equal, "", cap.stderr
159 nt.assert_equal("", cap.stderr)
148 yield nt.assert_equal, len(cap.outputs), 1
160 nt.assert_equal(len(cap.outputs), 1)
161
149
162
150 def test_capture_output_no_display():
163 def test_capture_output_no_display():
151 """test capture_output(display=False)"""
164 """test capture_output(display=False)"""
@@ -154,6 +167,6 b' def test_capture_output_no_display():'
154 print(hello_stdout, end="")
167 print(hello_stdout, end="")
155 print(hello_stderr, end="", file=sys.stderr)
168 print(hello_stderr, end="", file=sys.stderr)
156 rich.display()
169 rich.display()
157 yield nt.assert_equal, hello_stdout, cap.stdout
170 nt.assert_equal(hello_stdout, cap.stdout)
158 yield nt.assert_equal, hello_stderr, cap.stderr
171 nt.assert_equal(hello_stderr, cap.stderr)
159 yield nt.assert_equal, cap.outputs, [] No newline at end of file
172 nt.assert_equal(cap.outputs, [])
@@ -20,15 +20,23 b' Authors'
20 # third party
20 # third party
21 import nose.tools as nt
21 import nose.tools as nt
22
22
23 from IPython.testing.decorators import skip_iptest_but_not_pytest
24
23 # our own
25 # our own
24 from IPython.utils.PyColorize import Parser
26 from IPython.utils.PyColorize import Parser
25 import io
27 import io
28 import pytest
29
30
31 @pytest.fixture(scope="module", params=("Linux", "NoColor", "LightBG", "Neutral"))
32 def style(request):
33 yield request.param
26
34
27 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
28 # Test functions
36 # Test functions
29 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
30
38
31 sample = u"""
39 sample = """
32 def function(arg, *args, kwarg=True, **kwargs):
40 def function(arg, *args, kwarg=True, **kwargs):
33 '''
41 '''
34 this is docs
42 this is docs
@@ -47,32 +55,22 b' class Bar(Super):'
47 super(Bar, self).__init__(1**2, 3^4, 5 or 6)
55 super(Bar, self).__init__(1**2, 3^4, 5 or 6)
48 """
56 """
49
57
50 def test_loop_colors():
51
52 for style in ('Linux', 'NoColor','LightBG', 'Neutral'):
53
54 def test_unicode_colorize():
55 p = Parser(style=style)
56 f1 = p.format('1/0', 'str')
57 f2 = p.format(u'1/0', 'str')
58 nt.assert_equal(f1, f2)
59
58
60 def test_parse_sample():
59 @skip_iptest_but_not_pytest
61 """and test writing to a buffer"""
60 def test_parse_sample(style):
62 buf = io.StringIO()
61 """and test writing to a buffer"""
63 p = Parser(style=style)
62 buf = io.StringIO()
64 p.format(sample, buf)
63 p = Parser(style=style)
65 buf.seek(0)
64 p.format(sample, buf)
66 f1 = buf.read()
65 buf.seek(0)
66 f1 = buf.read()
67
67
68 nt.assert_not_in('ERROR', f1)
68 nt.assert_not_in("ERROR", f1)
69
69
70 def test_parse_error():
71 p = Parser(style=style)
72 f1 = p.format(')', 'str')
73 if style != 'NoColor':
74 nt.assert_in('ERROR', f1)
75
70
76 yield test_unicode_colorize
71 @skip_iptest_but_not_pytest
77 yield test_parse_sample
72 def test_parse_error(style):
78 yield test_parse_error
73 p = Parser(style=style)
74 f1 = p.format(")", "str")
75 if style != "NoColor":
76 nt.assert_in("ERROR", f1)
@@ -3,6 +3,8 b''
3 # Distributed under the terms of the Modified BSD License.
3 # Distributed under the terms of the Modified BSD License.
4
4
5 import nose.tools as nt
5 import nose.tools as nt
6 import pytest
7 from IPython.testing.decorators import skip_iptest_but_not_pytest
6
8
7 from IPython.utils.tokenutil import token_at_cursor, line_at_cursor
9 from IPython.utils.tokenutil import token_at_cursor, line_at_cursor
8
10
@@ -120,14 +122,20 b' def test_line_at_cursor():'
120 nt.assert_equal(line, "pri")
122 nt.assert_equal(line, "pri")
121 nt.assert_equal(offset, 4)
123 nt.assert_equal(offset, 4)
122
124
123 def test_multiline_statement():
125
126 @pytest.mark.parametrize(
127 "c, token",
128 zip(
129 list(range(16, 22)) + list(range(22, 28)),
130 ["int"] * (22 - 16) + ["map"] * (28 - 22),
131 ),
132 )
133 @skip_iptest_but_not_pytest
134 def test_multiline_statement(c, token):
124 cell = """a = (1,
135 cell = """a = (1,
125 3)
136 3)
126
137
127 int()
138 int()
128 map()
139 map()
129 """
140 """
130 for c in range(16, 22):
141 expect_token(token, cell, c)
131 yield lambda cell, c: expect_token("int", cell, c), cell, c
132 for c in range(22, 28):
133 yield lambda cell, c: expect_token("map", cell, c), cell, c
@@ -23,7 +23,7 b' init:'
23 install:
23 install:
24 - "SET PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%"
24 - "SET PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%"
25 - "%CMD_IN_ENV% python -m pip install --upgrade setuptools pip"
25 - "%CMD_IN_ENV% python -m pip install --upgrade setuptools pip"
26 - "%CMD_IN_ENV% pip install nose coverage"
26 - "%CMD_IN_ENV% pip install nose coverage pytest"
27 - "%CMD_IN_ENV% pip install .[test]"
27 - "%CMD_IN_ENV% pip install .[test]"
28 - "%CMD_IN_ENV% mkdir results"
28 - "%CMD_IN_ENV% mkdir results"
29 - "%CMD_IN_ENV% cd results"
29 - "%CMD_IN_ENV% cd results"
General Comments 0
You need to be logged in to leave comments. Login now