Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6958988

Browse files
authored
Merge pull request #12666 from Carreau/pytest-yields
2 parents 576a411 + 935cd31 commit 6958988

9 files changed

Lines changed: 157 additions & 94 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ install:
4141
fi
4242
- pip install -e file://$PWD#egg=ipython[test] --upgrade
4343
- 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'
4545
- pip install codecov check-manifest pytest-cov --upgrade
4646

4747

IPython/lib/latextools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def latex_to_png_dvipng(s, wrap, color='Black', scale=1.0):
138138
except FindCmdError:
139139
return None
140140
try:
141-
workdir = PurePath(tempfile.mkdtemp())
141+
workdir = Path(tempfile.mkdtemp())
142142
tmpfile = workdir.joinpath("tmp.tex")
143143
dvifile = workdir.joinpath("tmp.dvi")
144144
outfile = workdir.joinpath("tmp.png")

IPython/lib/tests/test_latextools.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
import pytest
1010

1111
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+
)
1317
from IPython.utils.process import FindCmdError
1418

1519

1620
@pytest.mark.parametrize('command', ['latex', 'dvipng'])
21+
@skip_iptest_but_not_pytest
1722
def test_check_latex_to_png_dvipng_fails_when_no_cmd(command):
1823
def mock_find_cmd(arg):
1924
if arg == command:
@@ -23,45 +28,48 @@ def mock_find_cmd(arg):
2328
assert latextools.latex_to_png_dvipng("whatever", True) == None
2429

2530

26-
@onlyif_cmds_exist('latex', 'dvipng')
27-
def test_latex_to_png_dvipng_runs():
31+
@contextmanager
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):
2840
"""
2941
Test that latex_to_png_dvipng just runs without error.
3042
"""
3143
def mock_kpsewhich(filename):
3244
assert filename == "breqn.sty"
3345
return None
3446

35-
for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]:
36-
yield (latextools.latex_to_png_dvipng, s, wrap)
47+
latextools.latex_to_png_dvipng(s, wrap)
3748

38-
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
39-
yield (latextools.latex_to_png_dvipng, s, wrap)
49+
with patch_latextool(mock_kpsewhich):
50+
latextools.latex_to_png_dvipng(s, wrap)
4051

4152

42-
@contextmanager
43-
def no_op(*args, **kwargs):
44-
yield
45-
4653
def mock_kpsewhich(filename):
47-
assert filename == "breqn.sty"
48-
return None
54+
assert filename == "breqn.sty"
55+
return None
4956

5057
@contextmanager
51-
def patch_latextool():
52-
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
58+
def patch_latextool(mock=mock_kpsewhich):
59+
with patch.object(latextools, "kpsewhich", mock):
5360
yield
5461

5562
@pytest.mark.parametrize('context', [no_op, patch_latextool])
5663
@pytest.mark.parametrize('s_wrap', [("$x^2$", False), ("x^2", True)])
64+
@skip_iptest_but_not_pytest
5765
def test_latex_to_png_mpl_runs(s_wrap, context):
5866
"""
5967
Test that latex_to_png_mpl just runs without error.
6068
"""
6169
try:
62-
import matplotbli
70+
import matplotlib
6371
except ImportError:
64-
pytest.skip("This needs matplotlib to be availlable")
72+
pytest.skip("This needs matplotlib to be available")
6573
return
6674
s, wrap = s_wrap
6775
with context():
@@ -81,7 +89,7 @@ def mock_kpsewhich(filename):
8189
assert False, ("kpsewhich should not be called "
8290
"(called with {0})".format(filename))
8391

84-
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
92+
with patch_latextool(mock_kpsewhich):
8593
assert '\n'.join(latextools.genelatex("body text", False)) == r'''\documentclass{article}
8694
\usepackage{amsmath}
8795
\usepackage{amsthm}
@@ -101,7 +109,7 @@ def mock_kpsewhich(filename):
101109
assert filename == "breqn.sty"
102110
return "path/to/breqn.sty"
103111

104-
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
112+
with patch_latextool(mock_kpsewhich):
105113
assert '\n'.join(latextools.genelatex("x^2", True)) == r'''\documentclass{article}
106114
\usepackage{amsmath}
107115
\usepackage{amsthm}
@@ -124,7 +132,7 @@ def mock_kpsewhich(filename):
124132
assert filename == "breqn.sty"
125133
return None
126134

127-
with patch.object(latextools, "kpsewhich", mock_kpsewhich):
135+
with patch_latextool(mock_kpsewhich):
128136
assert '\n'.join(latextools.genelatex("x^2", True)) == r'''\documentclass{article}
129137
\usepackage{amsmath}
130138
\usepackage{amsthm}

IPython/lib/tests/test_pretty.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
import unittest
1313

1414
import nose.tools as nt
15+
import pytest
1516

1617
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
1819

1920
from io import StringIO
2021

@@ -105,17 +106,36 @@ def test_callability_checking():
105106
nt.assert_equal(gotoutput, expectedoutput)
106107

107108

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):
109134
"""
110135
Test that set and frozenset use Python 3 formatting.
111136
"""
112-
objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]),
113-
frozenset([1, 2]), set([-1, -2, -3])]
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
137+
got_output = pretty.pretty(obj)
138+
nt.assert_equal(got_output, expected_output)
119139

120140

121141
@skip_without('xxlimited')

IPython/testing/decorators.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,17 @@ def decor(f):
154154
return decor
155155

156156

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+
157168
# Inspired by numpy's skipif, but uses the full apply_wrapper utility to
158169
# preserve function metadata better and allows the skip condition to be a
159170
# callable.
@@ -365,9 +376,14 @@ def onlyif_cmds_exist(*commands):
365376
Decorator to skip test when at least one of `commands` is not found.
366377
"""
367378
for cmd in commands:
379+
reason = "This test runs only if command '{cmd}' is installed"
368380
if not shutil.which(cmd):
369-
return skip("This test runs only if command '{0}' "
370-
"is installed".format(cmd))
381+
if os.environ.get("IPTEST_WORKING_DIR", None) is not None:
382+
return skip(reason)
383+
else:
384+
import pytest
385+
386+
return pytest.mark.skip(reason=reason)
371387
return null_deco
372388

373389
def onlyif_any_cmd_exists(*commands):

IPython/utils/tests/test_capture.py

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import sys
1717

1818
import nose.tools as nt
19+
import pytest
20+
21+
from IPython.testing.decorators import skip_iptest_but_not_pytest
1922

2023
from IPython.utils import capture
2124

@@ -66,38 +69,45 @@
6669
#-----------------------------------------------------------------------------
6770
# Test Functions
6871
#-----------------------------------------------------------------------------
69-
70-
def test_rich_output_empty():
72+
@pytest.mark.parametrize("method_mime", _mime_map.items())
73+
@skip_iptest_but_not_pytest
74+
def test_rich_output_empty(method_mime):
7175
"""RichOutput with no args"""
7276
rich = capture.RichOutput()
73-
for method, mime in _mime_map.items():
74-
yield nt.assert_equal, getattr(rich, method)(), None
77+
method, mime = method_mime
78+
nt.assert_equal(getattr(rich, method)(), None)
7579

7680
def test_rich_output():
7781
"""test RichOutput basics"""
7882
data = basic_data
7983
metadata = basic_metadata
8084
rich = capture.RichOutput(data=data, metadata=metadata)
81-
yield nt.assert_equal, rich._repr_html_(), data['text/html']
82-
yield nt.assert_equal, rich._repr_png_(), (data['image/png'], metadata['image/png'])
83-
yield nt.assert_equal, rich._repr_latex_(), None
84-
yield nt.assert_equal, rich._repr_javascript_(), None
85-
yield nt.assert_equal, rich._repr_svg_(), None
85+
nt.assert_equal(rich._repr_html_(), data["text/html"])
86+
nt.assert_equal(rich._repr_png_(), (data["image/png"], metadata["image/png"]))
87+
nt.assert_equal(rich._repr_latex_(), None)
88+
nt.assert_equal(rich._repr_javascript_(), None)
89+
nt.assert_equal(rich._repr_svg_(), None)
8690

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):
8895
"""test RichOutput with no metadata"""
8996
data = full_data
9097
rich = capture.RichOutput(data=data)
91-
for method, mime in _mime_map.items():
92-
yield nt.assert_equal, getattr(rich, method)(), data[mime]
98+
method, mime = method_mime
99+
nt.assert_equal(getattr(rich, method)(), data[mime])
100+
93101

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):
95105
"""test RichOutput with metadata"""
96106
data = full_data
97107
metadata = full_metadata
98108
rich = capture.RichOutput(data=data, metadata=metadata)
99-
for method, mime in _mime_map.items():
100-
yield nt.assert_equal, getattr(rich, method)(), (data[mime], metadata[mime])
109+
method, mime = method_mime
110+
nt.assert_equal(getattr(rich, method)(), (data[mime], metadata[mime]))
101111

102112
def test_rich_output_display():
103113
"""test RichOutput.display
@@ -109,10 +119,10 @@ def test_rich_output_display():
109119
rich = capture.RichOutput(data=data)
110120
with capture.capture_output() as cap:
111121
rich.display()
112-
yield nt.assert_equal, len(cap.outputs), 1
122+
nt.assert_equal(len(cap.outputs), 1)
113123
rich2 = cap.outputs[0]
114-
yield nt.assert_equal, rich2.data, rich.data
115-
yield nt.assert_equal, rich2.metadata, rich.metadata
124+
nt.assert_equal(rich2.data, rich.data)
125+
nt.assert_equal(rich2.metadata, rich.metadata)
116126

117127
def test_capture_output():
118128
"""capture_output works"""
@@ -121,8 +131,9 @@ def test_capture_output():
121131
print(hello_stdout, end="")
122132
print(hello_stderr, end="", file=sys.stderr)
123133
rich.display()
124-
yield nt.assert_equal, hello_stdout, cap.stdout
125-
yield nt.assert_equal, hello_stderr, cap.stderr
134+
nt.assert_equal(hello_stdout, cap.stdout)
135+
nt.assert_equal(hello_stderr, cap.stderr)
136+
126137

127138
def test_capture_output_no_stdout():
128139
"""test capture_output(stdout=False)"""
@@ -131,9 +142,10 @@ def test_capture_output_no_stdout():
131142
print(hello_stdout, end="")
132143
print(hello_stderr, end="", file=sys.stderr)
133144
rich.display()
134-
yield nt.assert_equal, "", cap.stdout
135-
yield nt.assert_equal, hello_stderr, cap.stderr
136-
yield nt.assert_equal, len(cap.outputs), 1
145+
nt.assert_equal("", cap.stdout)
146+
nt.assert_equal(hello_stderr, cap.stderr)
147+
nt.assert_equal(len(cap.outputs), 1)
148+
137149

138150
def test_capture_output_no_stderr():
139151
"""test capture_output(stderr=False)"""
@@ -143,9 +155,10 @@ def test_capture_output_no_stderr():
143155
print(hello_stdout, end="")
144156
print(hello_stderr, end="", file=sys.stderr)
145157
rich.display()
146-
yield nt.assert_equal, hello_stdout, cap.stdout
147-
yield nt.assert_equal, "", cap.stderr
148-
yield nt.assert_equal, len(cap.outputs), 1
158+
nt.assert_equal(hello_stdout, cap.stdout)
159+
nt.assert_equal("", cap.stderr)
160+
nt.assert_equal(len(cap.outputs), 1)
161+
149162

150163
def test_capture_output_no_display():
151164
"""test capture_output(display=False)"""
@@ -154,6 +167,6 @@ def test_capture_output_no_display():
154167
print(hello_stdout, end="")
155168
print(hello_stderr, end="", file=sys.stderr)
156169
rich.display()
157-
yield nt.assert_equal, hello_stdout, cap.stdout
158-
yield nt.assert_equal, hello_stderr, cap.stderr
159-
yield nt.assert_equal, cap.outputs, []
170+
nt.assert_equal(hello_stdout, cap.stdout)
171+
nt.assert_equal(hello_stderr, cap.stderr)
172+
nt.assert_equal(cap.outputs, [])

0 commit comments

Comments
 (0)