From 94b7db200f5040361204f723af824f384f77de00 2020-06-03 23:24:58 From: Matthias Bussonnier Date: 2020-06-03 23:24:58 Subject: [PATCH] Merge pull request #12362 from Carreau/pytest-ref Migrate some tests to pytest only, and start removing yield-tests. --- diff --git a/IPython/lib/tests/test_latextools.py b/IPython/lib/tests/test_latextools.py index 2cbdb17..419db42 100644 --- a/IPython/lib/tests/test_latextools.py +++ b/IPython/lib/tests/test_latextools.py @@ -1,32 +1,26 @@ -# encoding: utf-8 """Tests for IPython.utils.path.py""" - # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. + +from contextlib import contextmanager from unittest.mock import patch + import nose.tools as nt +import pytest from IPython.lib import latextools from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib from IPython.utils.process import FindCmdError -def test_latex_to_png_dvipng_fails_when_no_cmd(): - """ - `latex_to_png_dvipng` should return None when there is no required command - """ - for command in ['latex', 'dvipng']: - yield (check_latex_to_png_dvipng_fails_when_no_cmd, command) - - -def check_latex_to_png_dvipng_fails_when_no_cmd(command): +@pytest.mark.parametrize('command', ['latex', 'dvipng']) +def test_check_latex_to_png_dvipng_fails_when_no_cmd(command): def mock_find_cmd(arg): if arg == command: raise FindCmdError with patch.object(latextools, "find_cmd", mock_find_cmd): - nt.assert_equal(latextools.latex_to_png_dvipng("whatever", True), - None) + assert latextools.latex_to_png_dvipng("whatever", True) == None @onlyif_cmds_exist('latex', 'dvipng') @@ -35,7 +29,7 @@ def test_latex_to_png_dvipng_runs(): Test that latex_to_png_dvipng just runs without error. """ def mock_kpsewhich(filename): - nt.assert_equal(filename, "breqn.sty") + assert filename == "breqn.sty" return None for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]: @@ -44,25 +38,39 @@ def test_latex_to_png_dvipng_runs(): with patch.object(latextools, "kpsewhich", mock_kpsewhich): yield (latextools.latex_to_png_dvipng, s, wrap) -@skipif_not_matplotlib -def test_latex_to_png_mpl_runs(): - """ - Test that latex_to_png_mpl just runs without error. - """ - def mock_kpsewhich(filename): - nt.assert_equal(filename, "breqn.sty") + +@contextmanager +def no_op(*args, **kwargs): + yield + +def mock_kpsewhich(filename): + assert filename == "breqn.sty" return None - for (s, wrap) in [("$x^2$", False), ("x^2", True)]: - yield (latextools.latex_to_png_mpl, s, wrap) +@contextmanager +def patch_latextool(): + with patch.object(latextools, "kpsewhich", mock_kpsewhich): + yield - with patch.object(latextools, "kpsewhich", mock_kpsewhich): - yield (latextools.latex_to_png_mpl, s, wrap) +@pytest.mark.parametrize('context', [no_op, patch_latextool]) +@pytest.mark.parametrize('s_wrap', [("$x^2$", False), ("x^2", True)]) +def test_latex_to_png_mpl_runs(s_wrap, context): + """ + Test that latex_to_png_mpl just runs without error. + """ + try: + import matplotbli + except ImportError: + pytest.skip("This needs matplotlib to be availlable") + return + s, wrap = s_wrap + with context(): + latextools.latex_to_png_mpl(s, wrap) @skipif_not_matplotlib def test_latex_to_html(): img = latextools.latex_to_html("$x^2$") - nt.assert_in("data:image/png;base64,iVBOR", img) + assert "data:image/png;base64,iVBOR" in img def test_genelatex_no_wrap(): @@ -74,9 +82,7 @@ def test_genelatex_no_wrap(): "(called with {0})".format(filename)) with patch.object(latextools, "kpsewhich", mock_kpsewhich): - nt.assert_equal( - '\n'.join(latextools.genelatex("body text", False)), - r'''\documentclass{article} + assert '\n'.join(latextools.genelatex("body text", False)) == r'''\documentclass{article} \usepackage{amsmath} \usepackage{amsthm} \usepackage{amssymb} @@ -84,7 +90,7 @@ def test_genelatex_no_wrap(): \pagestyle{empty} \begin{document} body text -\end{document}''') +\end{document}''' def test_genelatex_wrap_with_breqn(): @@ -92,13 +98,11 @@ def test_genelatex_wrap_with_breqn(): Test genelatex with wrap=True for the case breqn.sty is installed. """ def mock_kpsewhich(filename): - nt.assert_equal(filename, "breqn.sty") + assert filename == "breqn.sty" return "path/to/breqn.sty" with patch.object(latextools, "kpsewhich", mock_kpsewhich): - nt.assert_equal( - '\n'.join(latextools.genelatex("x^2", True)), - r'''\documentclass{article} + assert '\n'.join(latextools.genelatex("x^2", True)) == r'''\documentclass{article} \usepackage{amsmath} \usepackage{amsthm} \usepackage{amssymb} @@ -109,7 +113,7 @@ def test_genelatex_wrap_with_breqn(): \begin{dmath*} x^2 \end{dmath*} -\end{document}''') +\end{document}''' def test_genelatex_wrap_without_breqn(): @@ -117,13 +121,11 @@ def test_genelatex_wrap_without_breqn(): Test genelatex with wrap=True for the case breqn.sty is not installed. """ def mock_kpsewhich(filename): - nt.assert_equal(filename, "breqn.sty") + assert filename == "breqn.sty" return None with patch.object(latextools, "kpsewhich", mock_kpsewhich): - nt.assert_equal( - '\n'.join(latextools.genelatex("x^2", True)), - r'''\documentclass{article} + assert '\n'.join(latextools.genelatex("x^2", True)) == r'''\documentclass{article} \usepackage{amsmath} \usepackage{amsthm} \usepackage{amssymb} @@ -131,7 +133,7 @@ def test_genelatex_wrap_without_breqn(): \pagestyle{empty} \begin{document} $$x^2$$ -\end{document}''') +\end{document}''' @skipif_not_matplotlib @@ -146,28 +148,28 @@ def test_latex_to_png_color(): color='#000000') dvipng_default = latextools.latex_to_png_dvipng(latex_string, False) dvipng_black = latextools.latex_to_png_dvipng(latex_string, False, 'Black') - nt.assert_equal(dvipng_default, dvipng_black) + assert dvipng_default == dvipng_black mpl_default = latextools.latex_to_png_mpl(latex_string, False) mpl_black = latextools.latex_to_png_mpl(latex_string, False, 'Black') - nt.assert_equal(mpl_default, mpl_black) - nt.assert_in(default_value, [dvipng_black, mpl_black]) - nt.assert_in(default_hexblack, [dvipng_black, mpl_black]) + assert mpl_default == mpl_black + assert default_value in [dvipng_black, mpl_black] + assert default_hexblack in [dvipng_black, mpl_black] # Test that dvips name colors can be used without error dvipng_maroon = latextools.latex_to_png_dvipng(latex_string, False, 'Maroon') # And that it doesn't return the black one - nt.assert_not_equal(dvipng_black, dvipng_maroon) + assert dvipng_black != dvipng_maroon mpl_maroon = latextools.latex_to_png_mpl(latex_string, False, 'Maroon') - nt.assert_not_equal(mpl_black, mpl_maroon) + assert mpl_black != mpl_maroon mpl_white = latextools.latex_to_png_mpl(latex_string, False, 'White') mpl_hexwhite = latextools.latex_to_png_mpl(latex_string, False, '#FFFFFF') - nt.assert_equal(mpl_white, mpl_hexwhite) + assert mpl_white == mpl_hexwhite mpl_white_scale = latextools.latex_to_png_mpl(latex_string, False, 'White', 1.2) - nt.assert_not_equal(mpl_white, mpl_white_scale) + assert mpl_white != mpl_white_scale def test_latex_to_png_invalid_hex_colors(): diff --git a/IPython/testing/iptest.py b/IPython/testing/iptest.py index 9bdaefd..8b7e34c 100644 --- a/IPython/testing/iptest.py +++ b/IPython/testing/iptest.py @@ -183,7 +183,8 @@ if not have['matplotlib']: # lib: sec = test_sections['lib'] -sec.exclude('kernel') +sec.exclude('tests.test_latextools') +#sec.exclude('kernel') if not have['pygments']: sec.exclude('tests.test_lexers') # We do this unconditionally, so that the test suite doesn't import @@ -210,8 +211,6 @@ test_sections['terminal'].exclude('console') # extensions: sec = test_sections['extensions'] -# This is deprecated in favour of rpy2 -sec.exclude('rmagic') # autoreload does some strange stuff, so move it to its own test section sec.exclude('autoreload') sec.exclude('tests.test_autoreload')