Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REF/TST: replace capture_stdout with pytest capsys fixture #24501

Merged
merged 3 commits into from
Dec 30, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pandas/tests/frame/test_repr_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/io/formats/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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(
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
22 changes: 10 additions & 12 deletions pandas/tests/io/parser/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down
2 changes: 0 additions & 2 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2310,7 +2310,6 @@ def test_schema(self):
cur = self.conn.cursor()
cur.execute(create_sql)

@tm.capture_stdout
jreback marked this conversation as resolved.
Show resolved Hide resolved
def test_execute_fail(self):
create_sql = """
CREATE TABLE test
Expand Down Expand Up @@ -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 = """
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
51 changes: 2 additions & 49 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from pandas._libs import testing as _testing
import pandas.compat as compat
from pandas.compat import (
PY2, PY3, Counter, StringIO, callable, filter, httplib, lmap, lrange, lzip,
map, raise_with_traceback, range, string_types, u, unichr, zip)
PY2, PY3, Counter, callable, filter, httplib, lmap, lrange, lzip, map,
raise_with_traceback, range, string_types, u, unichr, zip)

from pandas.core.dtypes.common import (
is_bool, is_categorical_dtype, is_datetime64_dtype, is_datetime64tz_dtype,
Expand Down Expand Up @@ -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


# -----------------------------------------------------------------------------
# Console debugging tools

Expand Down