Skip to content

Commit 032a260

Browse files
simonjayhawkinsPingviinituutti
authored andcommitted
REF/TST: replace capture_stdout with pytest capsys fixture (pandas-dev#24501)
1 parent 970b48c commit 032a260

File tree

8 files changed

+15
-72
lines changed

8 files changed

+15
-72
lines changed

pandas/tests/frame/test_repr_info.py

-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ def test_latex_repr(self):
193193
# GH 12182
194194
assert df._repr_latex_() is None
195195

196-
@tm.capture_stdout
197196
def test_info(self):
198197
io = StringIO()
199198
self.frame.info(buf=io)

pandas/tests/io/formats/test_to_csv.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,7 @@ def test_to_csv_string_with_crlf(self):
459459
with open(path, 'rb') as f:
460460
assert f.read() == expected_crlf
461461

462-
@tm.capture_stdout
463-
def test_to_csv_stdout_file(self):
462+
def test_to_csv_stdout_file(self, capsys):
464463
# GH 21561
465464
df = pd.DataFrame([['foo', 'bar'], ['baz', 'qux']],
466465
columns=['name_1', 'name_2'])
@@ -470,9 +469,9 @@ def test_to_csv_stdout_file(self):
470469
expected_ascii = tm.convert_rows_list_to_csv_str(expected_rows)
471470

472471
df.to_csv(sys.stdout, encoding='ascii')
473-
output = sys.stdout.getvalue()
472+
captured = capsys.readouterr()
474473

475-
assert output == expected_ascii
474+
assert captured.out == expected_ascii
476475
assert not sys.stdout.closed
477476

478477
@pytest.mark.xfail(

pandas/tests/io/formats/test_to_html.py

-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ def test_to_html_border_zero(self):
246246
result = df.to_html(border=0)
247247
assert 'border="0"' in result
248248

249-
@tm.capture_stdout
250249
def test_display_option_warning(self):
251250
with tm.assert_produces_warning(FutureWarning,
252251
check_stacklevel=False):

pandas/tests/io/parser/test_common.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from datetime import datetime
1212
import os
1313
import platform
14-
import sys
1514
from tempfile import TemporaryFile
1615

1716
import numpy as np
@@ -1509,8 +1508,7 @@ def test_whitespace_regex_separator(all_parsers, data, expected):
15091508
tm.assert_frame_equal(result, expected)
15101509

15111510

1512-
@tm.capture_stdout
1513-
def test_verbose_read(all_parsers):
1511+
def test_verbose_read(all_parsers, capsys):
15141512
parser = all_parsers
15151513
data = """a,b,c,d
15161514
one,1,2,3
@@ -1524,17 +1522,16 @@ def test_verbose_read(all_parsers):
15241522

15251523
# Engines are verbose in different ways.
15261524
parser.read_csv(StringIO(data), verbose=True)
1527-
output = sys.stdout.getvalue()
1525+
captured = capsys.readouterr()
15281526

15291527
if parser.engine == "c":
1530-
assert "Tokenization took:" in output
1531-
assert "Parser memory cleanup took:" in output
1528+
assert "Tokenization took:" in captured.out
1529+
assert "Parser memory cleanup took:" in captured.out
15321530
else: # Python engine
1533-
assert output == "Filled 3 NA values in column a\n"
1531+
assert captured.out == "Filled 3 NA values in column a\n"
15341532

15351533

1536-
@tm.capture_stdout
1537-
def test_verbose_read2(all_parsers):
1534+
def test_verbose_read2(all_parsers, capsys):
15381535
parser = all_parsers
15391536
data = """a,b,c,d
15401537
one,1,2,3
@@ -1547,14 +1544,14 @@ def test_verbose_read2(all_parsers):
15471544
eight,1,2,3"""
15481545

15491546
parser.read_csv(StringIO(data), verbose=True, index_col=0)
1550-
output = sys.stdout.getvalue()
1547+
captured = capsys.readouterr()
15511548

15521549
# Engines are verbose in different ways.
15531550
if parser.engine == "c":
1554-
assert "Tokenization took:" in output
1555-
assert "Parser memory cleanup took:" in output
1551+
assert "Tokenization took:" in captured.out
1552+
assert "Parser memory cleanup took:" in captured.out
15561553
else: # Python engine
1557-
assert output == "Filled 1 NA values in column a\n"
1554+
assert captured.out == "Filled 1 NA values in column a\n"
15581555

15591556

15601557
def test_iteration_open_handle(all_parsers):

pandas/tests/io/test_sql.py

-2
Original file line numberDiff line numberDiff line change
@@ -2310,7 +2310,6 @@ def test_schema(self):
23102310
cur = self.conn.cursor()
23112311
cur.execute(create_sql)
23122312

2313-
@tm.capture_stdout
23142313
def test_execute_fail(self):
23152314
create_sql = """
23162315
CREATE TABLE test
@@ -2567,7 +2566,6 @@ def test_schema(self):
25672566
cur.execute(drop_sql)
25682567
cur.execute(create_sql)
25692568

2570-
@tm.capture_stdout
25712569
def test_execute_fail(self):
25722570
drop_sql = "DROP TABLE IF EXISTS test"
25732571
create_sql = """

pandas/tests/plotting/test_frame.py

-1
Original file line numberDiff line numberDiff line change
@@ -1819,7 +1819,6 @@ def test_line_label_none(self):
18191819
assert ax.get_legend().get_texts()[0].get_text() == 'None'
18201820

18211821
@pytest.mark.slow
1822-
@tm.capture_stdout
18231822
def test_line_colors(self):
18241823
from matplotlib import cm
18251824

pandas/tests/series/test_missing.py

-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,6 @@ def test_isna_for_inf(self):
475475
tm.assert_series_equal(r, e)
476476
tm.assert_series_equal(dr, de)
477477

478-
@tm.capture_stdout
479478
def test_isnull_for_inf_deprecated(self):
480479
# gh-17115
481480
s = Series(['a', np.inf, np.nan, 1.0])

pandas/util/testing.py

+2-49
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
from pandas._libs import testing as _testing
2121
import pandas.compat as compat
2222
from pandas.compat import (
23-
PY2, PY3, Counter, StringIO, callable, filter, httplib, lmap, lrange, lzip,
24-
map, raise_with_traceback, range, string_types, u, unichr, zip)
23+
PY2, PY3, Counter, callable, filter, httplib, lmap, lrange, lzip, map,
24+
raise_with_traceback, range, string_types, u, unichr, zip)
2525

2626
from pandas.core.dtypes.common import (
2727
is_bool, is_categorical_dtype, is_datetime64_dtype, is_datetime64tz_dtype,
@@ -637,53 +637,6 @@ def set_defaultencoding(encoding):
637637
sys.setdefaultencoding(orig)
638638

639639

640-
def capture_stdout(f):
641-
r"""
642-
Decorator to capture stdout in a buffer so that it can be checked
643-
(or suppressed) during testing.
644-
645-
Parameters
646-
----------
647-
f : callable
648-
The test that is capturing stdout.
649-
650-
Returns
651-
-------
652-
f : callable
653-
The decorated test ``f``, which captures stdout.
654-
655-
Examples
656-
--------
657-
658-
>>> from pandas.util.testing import capture_stdout
659-
>>> import sys
660-
>>>
661-
>>> @capture_stdout
662-
... def test_print_pass():
663-
... print("foo")
664-
... out = sys.stdout.getvalue()
665-
... assert out == "foo\n"
666-
>>>
667-
>>> @capture_stdout
668-
... def test_print_fail():
669-
... print("foo")
670-
... out = sys.stdout.getvalue()
671-
... assert out == "bar\n"
672-
...
673-
AssertionError: assert 'foo\n' == 'bar\n'
674-
"""
675-
676-
@compat.wraps(f)
677-
def wrapper(*args, **kwargs):
678-
try:
679-
sys.stdout = StringIO()
680-
f(*args, **kwargs)
681-
finally:
682-
sys.stdout = sys.__stdout__
683-
684-
return wrapper
685-
686-
687640
# -----------------------------------------------------------------------------
688641
# Console debugging tools
689642

0 commit comments

Comments
 (0)