From 8e0b595048303df835696f7545636f0f33f5b887 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 30 Dec 2018 09:15:18 +0000 Subject: [PATCH 1/9] use capsys in test_map_fallthrough --- pandas/tests/indexes/datetimes/test_datetime.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_datetime.py b/pandas/tests/indexes/datetimes/test_datetime.py index c338026025767..e76de2ebedf67 100644 --- a/pandas/tests/indexes/datetimes/test_datetime.py +++ b/pandas/tests/indexes/datetimes/test_datetime.py @@ -1,5 +1,4 @@ from datetime import date -import sys import dateutil import numpy as np @@ -125,15 +124,14 @@ def test_map(self): exp = Index([f(x) for x in rng], dtype=' Date: Sun, 30 Dec 2018 09:23:29 +0000 Subject: [PATCH 2/9] use capsys in test_comment_whitespace_delimited --- pandas/tests/io/parser/test_c_parser_only.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/tests/io/parser/test_c_parser_only.py b/pandas/tests/io/parser/test_c_parser_only.py index fcf9736110ff8..76a04a4161625 100644 --- a/pandas/tests/io/parser/test_c_parser_only.py +++ b/pandas/tests/io/parser/test_c_parser_only.py @@ -10,7 +10,6 @@ from io import TextIOWrapper import mmap import os -import sys import tarfile import numpy as np @@ -449,8 +448,7 @@ def test_data_after_quote(c_parser_only): tm.assert_frame_equal(result, expected) -@tm.capture_stderr -def test_comment_whitespace_delimited(c_parser_only): +def test_comment_whitespace_delimited(c_parser_only, capsys): parser = c_parser_only test_input = """\ 1 2 @@ -466,10 +464,10 @@ def test_comment_whitespace_delimited(c_parser_only): df = parser.read_csv(StringIO(test_input), comment="#", header=None, delimiter="\\s+", skiprows=0, error_bad_lines=False) - error = sys.stderr.getvalue() + captured = capsys.readouterr() # skipped lines 2, 3, 4, 9 for line_num in (2, 3, 4, 9): - assert "Skipping line {}".format(line_num) in error, error + assert "Skipping line {}".format(line_num) in captured.err expected = DataFrame([[1, 2], [5, 2], [6, 2], From aef1ffb24b40d3c99453e8712186946e629e09f4 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 30 Dec 2018 09:54:31 +0000 Subject: [PATCH 3/9] use capsys in test_warn_bad_lines --- pandas/tests/io/parser/test_common.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/tests/io/parser/test_common.py b/pandas/tests/io/parser/test_common.py index 9d38fdbecdb62..0bf25c2e76406 100644 --- a/pandas/tests/io/parser/test_common.py +++ b/pandas/tests/io/parser/test_common.py @@ -1867,8 +1867,7 @@ def test_error_bad_lines(all_parsers, kwargs, warn_kwargs): parser.read_csv(StringIO(data), **kwargs) -@tm.capture_stderr -def test_warn_bad_lines(all_parsers): +def test_warn_bad_lines(all_parsers, capsys): # see gh-15925 parser = all_parsers data = "a\n1\n1,2,3\n4\n5,6,7" @@ -1879,9 +1878,9 @@ def test_warn_bad_lines(all_parsers): warn_bad_lines=True) tm.assert_frame_equal(result, expected) - val = sys.stderr.getvalue() - assert "Skipping line 3" in val - assert "Skipping line 5" in val + captured = capsys.readouterr() + assert "Skipping line 3" in captured.err + assert "Skipping line 5" in captured.err @tm.capture_stderr From 8580c2432e0330b2252f0ed3983e31f4a5f758e7 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 30 Dec 2018 09:58:35 +0000 Subject: [PATCH 4/9] use capsys in test_suppress_error_output --- pandas/tests/io/parser/test_common.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/parser/test_common.py b/pandas/tests/io/parser/test_common.py index 0bf25c2e76406..9471a3a91086f 100644 --- a/pandas/tests/io/parser/test_common.py +++ b/pandas/tests/io/parser/test_common.py @@ -1883,8 +1883,7 @@ def test_warn_bad_lines(all_parsers, capsys): assert "Skipping line 5" in captured.err -@tm.capture_stderr -def test_suppress_error_output(all_parsers): +def test_suppress_error_output(all_parsers, capsys): # see gh-15925 parser = all_parsers data = "a\n1\n1,2,3\n4\n5,6,7" @@ -1895,8 +1894,8 @@ def test_suppress_error_output(all_parsers): warn_bad_lines=False) tm.assert_frame_equal(result, expected) - val = sys.stderr.getvalue() - assert val == "" + captured = capsys.readouterr() + assert captured.err == "" def test_read_table_deprecated(all_parsers): From 80de1e196b8f82474901604b02d12689578cb478 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 30 Dec 2018 10:04:22 +0000 Subject: [PATCH 5/9] use capsys in test_none_delimiter --- pandas/tests/io/parser/test_python_parser_only.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/tests/io/parser/test_python_parser_only.py b/pandas/tests/io/parser/test_python_parser_only.py index d5a7e3549ef0f..c2edff258f1b5 100644 --- a/pandas/tests/io/parser/test_python_parser_only.py +++ b/pandas/tests/io/parser/test_python_parser_only.py @@ -8,7 +8,6 @@ """ import csv -import sys import pytest @@ -248,8 +247,7 @@ def fail_read(): fail_read() -@tm.capture_stderr -def test_none_delimiter(python_parser_only): +def test_none_delimiter(python_parser_only, capsys): # see gh-13374 and gh-17465 parser = python_parser_only data = "a,b,c\n0,1,2\n3,4,5,6\n7,8,9" @@ -263,8 +261,8 @@ def test_none_delimiter(python_parser_only): error_bad_lines=False) tm.assert_frame_equal(result, expected) - warning = sys.stderr.getvalue() - assert "Skipping line 3" in warning + captured = capsys.readouterr() + assert "Skipping line 3" in captured.err @pytest.mark.parametrize("data", [ From ebfbe4accea9e066530bfb1d64f537344a86ab3d Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 30 Dec 2018 10:09:26 +0000 Subject: [PATCH 6/9] use capsys in test_skip_bad_lines --- pandas/tests/io/parser/test_textreader.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pandas/tests/io/parser/test_textreader.py b/pandas/tests/io/parser/test_textreader.py index 93c115ae0a57b..0fd92cb496df3 100644 --- a/pandas/tests/io/parser/test_textreader.py +++ b/pandas/tests/io/parser/test_textreader.py @@ -6,7 +6,6 @@ """ import os -import sys import numpy as np from numpy import nan @@ -135,8 +134,7 @@ def test_integer_thousands_alt(self): expected = DataFrame([123456, 12500]) tm.assert_frame_equal(result, expected) - @tm.capture_stderr - def test_skip_bad_lines(self): + def test_skip_bad_lines(self, capsys): # too many lines, see #2430 for why data = ('a:b:c\n' 'd:e:f\n' @@ -164,10 +162,10 @@ def test_skip_bad_lines(self): error_bad_lines=False, warn_bad_lines=True) reader.read() - val = sys.stderr.getvalue() + captured = capsys.readouterr() - assert 'Skipping line 4' in val - assert 'Skipping line 6' in val + assert 'Skipping line 4' in captured.err + assert 'Skipping line 6' in captured.err def test_header_not_enough_lines(self): data = ('skip this\n' From dcdb779805619c622e95e5ba9955bae88cc0c18d Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 30 Dec 2018 10:15:17 +0000 Subject: [PATCH 7/9] use capsys in test_repr_bool_fails --- pandas/tests/series/test_repr.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index 86de8176a9a65..b4e7708e2456e 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -2,7 +2,6 @@ # pylint: disable-msg=E1101,W0612 from datetime import datetime, timedelta -import sys import numpy as np @@ -121,15 +120,14 @@ def test_tidy_repr(self): a.name = 'title1' repr(a) # should not raise exception - @tm.capture_stderr - def test_repr_bool_fails(self): + def test_repr_bool_fails(self, capsys): s = Series([DataFrame(np.random.randn(2, 2)) for i in range(5)]) # It works (with no Cython exception barf)! repr(s) - output = sys.stderr.getvalue() - assert output == '' + captured = capsys.readouterr() + assert captured.err == '' def test_repr_name_iterable_indexable(self): s = Series([1, 2, 3], name=np.int64(3)) From 31889950b44678bd49d4039e08d1bce34dc7eafd Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 30 Dec 2018 10:20:43 +0000 Subject: [PATCH 8/9] use capsys in test_validate_docstrings.py --- scripts/tests/test_validate_docstrings.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/scripts/tests/test_validate_docstrings.py b/scripts/tests/test_validate_docstrings.py index ca09cbb23d145..3d16fecb4ec3c 100644 --- a/scripts/tests/test_validate_docstrings.py +++ b/scripts/tests/test_validate_docstrings.py @@ -4,7 +4,6 @@ import textwrap import pytest import numpy as np -from pandas.util.testing import capture_stderr import validate_docstrings validate_one = validate_docstrings.validate_one @@ -739,36 +738,32 @@ def _import_path(self, klass=None, func=None): return base_path - @capture_stderr - def test_good_class(self): + def test_good_class(self, capsys): errors = validate_one(self._import_path( klass='GoodDocStrings'))['errors'] assert isinstance(errors, list) assert not errors - @capture_stderr @pytest.mark.parametrize("func", [ 'plot', 'sample', 'random_letters', 'sample_values', 'head', 'head1', 'contains', 'mode', 'good_imports']) - def test_good_functions(self, func): + def test_good_functions(self, capsys, func): errors = validate_one(self._import_path( klass='GoodDocStrings', func=func))['errors'] assert isinstance(errors, list) assert not errors - @capture_stderr - def test_bad_class(self): + def test_bad_class(self, capsys): errors = validate_one(self._import_path( klass='BadGenericDocStrings'))['errors'] assert isinstance(errors, list) assert errors - @capture_stderr @pytest.mark.parametrize("func", [ 'func', 'astype', 'astype1', 'astype2', 'astype3', 'plot', 'method', 'private_classes', ]) - def test_bad_generic_functions(self, func): + def test_bad_generic_functions(self, capsys, func): errors = validate_one(self._import_path( # noqa:F821 klass='BadGenericDocStrings', func=func))['errors'] assert isinstance(errors, list) From c54cf3e5585218aeab634db67b80c5d7771ecc49 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sun, 30 Dec 2018 10:22:25 +0000 Subject: [PATCH 9/9] remove capture_stderr --- pandas/util/testing.py | 46 ------------------------------------------ 1 file changed, 46 deletions(-) diff --git a/pandas/util/testing.py b/pandas/util/testing.py index 171d4d1ffcb39..91e3c6d64990b 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -684,52 +684,6 @@ def wrapper(*args, **kwargs): return wrapper -def capture_stderr(f): - r""" - Decorator to capture stderr in a buffer so that it can be checked - (or suppressed) during testing. - - Parameters - ---------- - f : callable - The test that is capturing stderr. - - Returns - ------- - f : callable - The decorated test ``f``, which captures stderr. - - Examples - -------- - - >>> from pandas.util.testing import capture_stderr - >>> import sys - >>> - >>> @capture_stderr - ... def test_stderr_pass(): - ... sys.stderr.write("foo") - ... out = sys.stderr.getvalue() - ... assert out == "foo\n" - >>> - >>> @capture_stderr - ... def test_stderr_fail(): - ... sys.stderr.write("foo") - ... out = sys.stderr.getvalue() - ... assert out == "bar\n" - ... - AssertionError: assert 'foo\n' == 'bar\n' - """ - - @compat.wraps(f) - def wrapper(*args, **kwargs): - try: - sys.stderr = StringIO() - f(*args, **kwargs) - finally: - sys.stderr = sys.__stderr__ - - return wrapper - # ----------------------------------------------------------------------------- # Console debugging tools