From b078d2569f255915e05f8a8f752c7a0c6953d854 2019-06-21 16:46:24 From: Matthias Bussonnier Date: 2019-06-21 16:46:24 Subject: [PATCH] Misc modifications for pytest comppatibility 1) remove objects creations `ip = get_ipython()` which is anyway injected in builtins. 2) use proper xunit test cases for capturing warnings. 3) Use proper assertWarns instead of assert print, as pytest captures warnings. 4) fix recusrsion limit in pytest whcih need to be higher 5) properly capture context in yielded tests. --- diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py index f57e9c2..23278ef 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -36,7 +36,6 @@ from IPython.utils.process import find_cmd # Globals #----------------------------------------------------------------------------- # This is used by every single test, no point repeating it ad nauseam -ip = get_ipython() #----------------------------------------------------------------------------- # Tests @@ -127,8 +126,8 @@ class InteractiveShellTestCase(unittest.TestCase): """Pretty-printing lists of objects with non-ascii reprs may cause problems.""" class Spam(object): - def __repr__(self): - return "\xe9"*50 + def __repr__(self): + return "\xe9"*50 import IPython.core.formatters f = IPython.core.formatters.PlainTextFormatter() f([Spam(),Spam()]) @@ -766,7 +765,7 @@ class TestAstTransformError(unittest.TestCase): err_transformer = ErrorTransformer() ip.ast_transformers.append(err_transformer) - with tt.AssertPrints("unregister", channel='stderr'): + with self.assertWarnsRegex(UserWarning, "It will be unregistered"): ip.run_cell("1 + 2") # This should have been removed. @@ -882,9 +881,6 @@ def test_user_expression(): # back to text only ip.display_formatter.active_types = ['text/plain'] - - - class TestSyntaxErrorTransformer(unittest.TestCase): @@ -918,25 +914,25 @@ class TestSyntaxErrorTransformer(unittest.TestCase): ip.run_cell('3456') - -def test_warning_suppression(): - ip.run_cell("import warnings") - try: - with tt.AssertPrints("UserWarning: asdf", channel="stderr"): - ip.run_cell("warnings.warn('asdf')") - # Here's the real test -- if we run that again, we should get the - # warning again. Traditionally, each warning was only issued once per - # IPython session (approximately), even if the user typed in new and - # different code that should have also triggered the warning, leading - # to much confusion. - with tt.AssertPrints("UserWarning: asdf", channel="stderr"): - ip.run_cell("warnings.warn('asdf')") - finally: - ip.run_cell("del warnings") +class TestWarningSupression(unittest.TestCase): + def test_warning_suppression(self): + ip.run_cell("import warnings") + try: + with self.assertWarnsRegex(UserWarning, "asdf"): + ip.run_cell("warnings.warn('asdf')") + # Here's the real test -- if we run that again, we should get the + # warning again. Traditionally, each warning was only issued once per + # IPython session (approximately), even if the user typed in new and + # different code that should have also triggered the warning, leading + # to much confusion. + with self.assertWarnsRegex(UserWarning, "asdf"): + ip.run_cell("warnings.warn('asdf')") + finally: + ip.run_cell("del warnings") -def test_deprecation_warning(): - ip.run_cell(""" + def test_deprecation_warning(self): + ip.run_cell(""" import warnings def wrn(): warnings.warn( @@ -944,12 +940,12 @@ def wrn(): DeprecationWarning ) """) - try: - with tt.AssertPrints("I AM A WARNING", channel="stderr"): - ip.run_cell("wrn()") - finally: - ip.run_cell("del warnings") - ip.run_cell("del wrn") + try: + with self.assertWarnsRegex(DeprecationWarning, "I AM A WARNING"): + ip.run_cell("wrn()") + finally: + ip.run_cell("del warnings") + ip.run_cell("del wrn") class TestImportNoDeprecate(tt.TempFileMixin): diff --git a/IPython/core/tests/test_magic_terminal.py b/IPython/core/tests/test_magic_terminal.py index a9dd7ac..0981c7b 100644 --- a/IPython/core/tests/test_magic_terminal.py +++ b/IPython/core/tests/test_magic_terminal.py @@ -18,7 +18,6 @@ from IPython.testing import tools as tt #----------------------------------------------------------------------------- # Globals #----------------------------------------------------------------------------- -ip = get_ipython() #----------------------------------------------------------------------------- # Test functions begin @@ -170,7 +169,7 @@ class PasteTestCase(TestCase): ip.write = writer nt.assert_equal(ip.user_ns['a'], 100) nt.assert_equal(ip.user_ns['b'], 200) - nt.assert_equal(out, code+"\n## -- End pasted text --\n") + assert out == code+"\n## -- End pasted text --\n" def test_paste_leading_commas(self): "Test multiline strings with leading commas" diff --git a/IPython/core/tests/test_ultratb.py b/IPython/core/tests/test_ultratb.py index 787a7ec..e77f444 100644 --- a/IPython/core/tests/test_ultratb.py +++ b/IPython/core/tests/test_ultratb.py @@ -39,7 +39,7 @@ def recursionlimit(frames): def inner(test_function): def wrapper(*args, **kwargs): _orig_rec_limit = ultratb._FRAME_RECURSION_LIMIT - ultratb._FRAME_RECURSION_LIMIT = frames - 50 + ultratb._FRAME_RECURSION_LIMIT = 50 rl = sys.getrecursionlimit() sys.setrecursionlimit(frames) @@ -327,17 +327,17 @@ def r3o2(): with tt.AssertNotPrints("frames repeated"): ip.run_cell("non_recurs()") - @recursionlimit(65) + @recursionlimit(150) def test_recursion_one_frame(self): with tt.AssertPrints("1 frames repeated"): ip.run_cell("r1()") - @recursionlimit(65) + @recursionlimit(150) def test_recursion_three_frames(self): with tt.AssertPrints("3 frames repeated"): ip.run_cell("r3o2()") - @recursionlimit(65) + @recursionlimit(150) def test_find_recursion(self): captured = [] def capture_exc(*args, **kwargs): diff --git a/IPython/utils/tests/test_tokenutil.py b/IPython/utils/tests/test_tokenutil.py index 253d0a4..4ffe7af 100644 --- a/IPython/utils/tests/test_tokenutil.py +++ b/IPython/utils/tests/test_tokenutil.py @@ -128,6 +128,6 @@ int() map() """ for c in range(16, 22): - yield lambda: expect_token("int", cell, c) + yield lambda cell, c: expect_token("int", cell, c), cell, c for c in range(22, 28): - yield lambda: expect_token("map", cell, c) + yield lambda cell, c: expect_token("map", cell, c), cell, c