From cc5726fe2ea83b27d707a827c2e5d4e9396ad9c5 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 30 Dec 2018 17:50:53 +0000 Subject: [PATCH 1/2] REF/TST: replace capture_stdout with pytest capsys fixture --- pandas/tests/frame/test_repr_info.py | 1 - pandas/tests/io/formats/test_to_csv.py | 7 ++-- pandas/tests/io/formats/test_to_html.py | 1 - pandas/tests/io/parser/test_common.py | 22 ++++++------ pandas/tests/io/test_sql.py | 2 -- pandas/tests/plotting/test_frame.py | 1 - pandas/tests/series/test_missing.py | 1 - pandas/util/testing.py | 47 ------------------------- 8 files changed, 13 insertions(+), 69 deletions(-) diff --git a/pandas/tests/frame/test_repr_info.py b/pandas/tests/frame/test_repr_info.py index 07cbb8cdcde0a..714b9b54ccb82 100644 --- a/pandas/tests/frame/test_repr_info.py +++ b/pandas/tests/frame/test_repr_info.py @@ -193,7 +193,6 @@ def test_latex_repr(self): # GH 12182 assert df._repr_latex_() is None - @tm.capture_stdout def test_info(self): io = StringIO() self.frame.info(buf=io) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index 69fdb7329a165..786c8fab08a01 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -459,8 +459,7 @@ def test_to_csv_string_with_crlf(self): with open(path, 'rb') as f: assert f.read() == expected_crlf - @tm.capture_stdout - def test_to_csv_stdout_file(self): + def test_to_csv_stdout_file(self, capsys): # GH 21561 df = pd.DataFrame([['foo', 'bar'], ['baz', 'qux']], columns=['name_1', 'name_2']) @@ -470,9 +469,9 @@ def test_to_csv_stdout_file(self): expected_ascii = tm.convert_rows_list_to_csv_str(expected_rows) df.to_csv(sys.stdout, encoding='ascii') - output = sys.stdout.getvalue() + captured = capsys.readouterr() - assert output == expected_ascii + assert captured.out == expected_ascii assert not sys.stdout.closed @pytest.mark.xfail( diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 9662b3d514cb8..6c2d12076a262 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -246,7 +246,6 @@ def test_to_html_border_zero(self): result = df.to_html(border=0) assert 'border="0"' in result - @tm.capture_stdout def test_display_option_warning(self): with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): diff --git a/pandas/tests/io/parser/test_common.py b/pandas/tests/io/parser/test_common.py index 9d38fdbecdb62..9970a15a0a928 100644 --- a/pandas/tests/io/parser/test_common.py +++ b/pandas/tests/io/parser/test_common.py @@ -1509,8 +1509,7 @@ def test_whitespace_regex_separator(all_parsers, data, expected): tm.assert_frame_equal(result, expected) -@tm.capture_stdout -def test_verbose_read(all_parsers): +def test_verbose_read(all_parsers, capsys): parser = all_parsers data = """a,b,c,d one,1,2,3 @@ -1524,17 +1523,16 @@ def test_verbose_read(all_parsers): # Engines are verbose in different ways. parser.read_csv(StringIO(data), verbose=True) - output = sys.stdout.getvalue() + captured = capsys.readouterr() if parser.engine == "c": - assert "Tokenization took:" in output - assert "Parser memory cleanup took:" in output + assert "Tokenization took:" in captured.out + assert "Parser memory cleanup took:" in captured.out else: # Python engine - assert output == "Filled 3 NA values in column a\n" + assert captured.out == "Filled 3 NA values in column a\n" -@tm.capture_stdout -def test_verbose_read2(all_parsers): +def test_verbose_read2(all_parsers, capsys): parser = all_parsers data = """a,b,c,d one,1,2,3 @@ -1547,14 +1545,14 @@ def test_verbose_read2(all_parsers): eight,1,2,3""" parser.read_csv(StringIO(data), verbose=True, index_col=0) - output = sys.stdout.getvalue() + captured = capsys.readouterr() # Engines are verbose in different ways. if parser.engine == "c": - assert "Tokenization took:" in output - assert "Parser memory cleanup took:" in output + assert "Tokenization took:" in captured.out + assert "Parser memory cleanup took:" in captured.out else: # Python engine - assert output == "Filled 1 NA values in column a\n" + assert captured.out == "Filled 1 NA values in column a\n" def test_iteration_open_handle(all_parsers): diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index c202fae8c91cf..24bd6d9ac4c51 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -2310,7 +2310,6 @@ def test_schema(self): cur = self.conn.cursor() cur.execute(create_sql) - @tm.capture_stdout def test_execute_fail(self): create_sql = """ CREATE TABLE test @@ -2567,7 +2566,6 @@ def test_schema(self): cur.execute(drop_sql) cur.execute(create_sql) - @tm.capture_stdout def test_execute_fail(self): drop_sql = "DROP TABLE IF EXISTS test" create_sql = """ diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 350d1bb153274..cc52130a10b2e 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -1819,7 +1819,6 @@ def test_line_label_none(self): assert ax.get_legend().get_texts()[0].get_text() == 'None' @pytest.mark.slow - @tm.capture_stdout def test_line_colors(self): from matplotlib import cm diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index dc58b46f90609..ffd21fb449864 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -475,7 +475,6 @@ def test_isna_for_inf(self): tm.assert_series_equal(r, e) tm.assert_series_equal(dr, de) - @tm.capture_stdout def test_isnull_for_inf_deprecated(self): # gh-17115 s = Series(['a', np.inf, np.nan, 1.0]) diff --git a/pandas/util/testing.py b/pandas/util/testing.py index 171d4d1ffcb39..349a155c260bb 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -637,53 +637,6 @@ def set_defaultencoding(encoding): sys.setdefaultencoding(orig) -def capture_stdout(f): - r""" - Decorator to capture stdout in a buffer so that it can be checked - (or suppressed) during testing. - - Parameters - ---------- - f : callable - The test that is capturing stdout. - - Returns - ------- - f : callable - The decorated test ``f``, which captures stdout. - - Examples - -------- - - >>> from pandas.util.testing import capture_stdout - >>> import sys - >>> - >>> @capture_stdout - ... def test_print_pass(): - ... print("foo") - ... out = sys.stdout.getvalue() - ... assert out == "foo\n" - >>> - >>> @capture_stdout - ... def test_print_fail(): - ... print("foo") - ... out = sys.stdout.getvalue() - ... assert out == "bar\n" - ... - AssertionError: assert 'foo\n' == 'bar\n' - """ - - @compat.wraps(f) - def wrapper(*args, **kwargs): - try: - sys.stdout = StringIO() - f(*args, **kwargs) - finally: - sys.stdout = sys.__stdout__ - - return wrapper - - def capture_stderr(f): r""" Decorator to capture stderr in a buffer so that it can be checked From cfdf3be8ab4744c5abe1a8d6901c16b5ae73e4c1 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 30 Dec 2018 20:42:46 +0000 Subject: [PATCH 2/2] error F401: 'sys' imported but unused --- pandas/tests/io/parser/test_common.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/io/parser/test_common.py b/pandas/tests/io/parser/test_common.py index e0b224e8ca965..2dc4c578102bb 100644 --- a/pandas/tests/io/parser/test_common.py +++ b/pandas/tests/io/parser/test_common.py @@ -11,7 +11,6 @@ from datetime import datetime import os import platform -import sys from tempfile import TemporaryFile import numpy as np