Skip to content

Commit 7e53d1a

Browse files
simonjayhawkinsPingviinituutti
authored andcommitted
REF/TST: replace capture_stderr with pytest capsys fixture (pandas-dev#24496)
1 parent 1ecab59 commit 7e53d1a

File tree

8 files changed

+27
-90
lines changed

8 files changed

+27
-90
lines changed

Diff for: pandas/tests/indexes/datetimes/test_datetime.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from datetime import date
2-
import sys
32

43
import dateutil
54
import numpy as np
@@ -125,15 +124,14 @@ def test_map(self):
125124
exp = Index([f(x) for x in rng], dtype='<U8')
126125
tm.assert_index_equal(result, exp)
127126

128-
@tm.capture_stderr
129-
def test_map_fallthrough(self):
127+
def test_map_fallthrough(self, capsys):
130128
# GH#22067, check we don't get warnings about silently ignored errors
131129
dti = date_range('2017-01-01', '2018-01-01', freq='B')
132130

133131
dti.map(lambda x: pd.Period(year=x.year, month=x.month, freq='M'))
134132

135-
cv = sys.stderr.getvalue()
136-
assert cv == ''
133+
captured = capsys.readouterr()
134+
assert captured.err == ''
137135

138136
def test_iteration_preserves_tz(self):
139137
# see gh-8890

Diff for: pandas/tests/io/parser/test_c_parser_only.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from io import TextIOWrapper
1111
import mmap
1212
import os
13-
import sys
1413
import tarfile
1514

1615
import numpy as np
@@ -449,8 +448,7 @@ def test_data_after_quote(c_parser_only):
449448
tm.assert_frame_equal(result, expected)
450449

451450

452-
@tm.capture_stderr
453-
def test_comment_whitespace_delimited(c_parser_only):
451+
def test_comment_whitespace_delimited(c_parser_only, capsys):
454452
parser = c_parser_only
455453
test_input = """\
456454
1 2
@@ -466,10 +464,10 @@ def test_comment_whitespace_delimited(c_parser_only):
466464
df = parser.read_csv(StringIO(test_input), comment="#", header=None,
467465
delimiter="\\s+", skiprows=0,
468466
error_bad_lines=False)
469-
error = sys.stderr.getvalue()
467+
captured = capsys.readouterr()
470468
# skipped lines 2, 3, 4, 9
471469
for line_num in (2, 3, 4, 9):
472-
assert "Skipping line {}".format(line_num) in error, error
470+
assert "Skipping line {}".format(line_num) in captured.err
473471
expected = DataFrame([[1, 2],
474472
[5, 2],
475473
[6, 2],

Diff for: pandas/tests/io/parser/test_common.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -1867,8 +1867,7 @@ def test_error_bad_lines(all_parsers, kwargs, warn_kwargs):
18671867
parser.read_csv(StringIO(data), **kwargs)
18681868

18691869

1870-
@tm.capture_stderr
1871-
def test_warn_bad_lines(all_parsers):
1870+
def test_warn_bad_lines(all_parsers, capsys):
18721871
# see gh-15925
18731872
parser = all_parsers
18741873
data = "a\n1\n1,2,3\n4\n5,6,7"
@@ -1879,13 +1878,12 @@ def test_warn_bad_lines(all_parsers):
18791878
warn_bad_lines=True)
18801879
tm.assert_frame_equal(result, expected)
18811880

1882-
val = sys.stderr.getvalue()
1883-
assert "Skipping line 3" in val
1884-
assert "Skipping line 5" in val
1881+
captured = capsys.readouterr()
1882+
assert "Skipping line 3" in captured.err
1883+
assert "Skipping line 5" in captured.err
18851884

18861885

1887-
@tm.capture_stderr
1888-
def test_suppress_error_output(all_parsers):
1886+
def test_suppress_error_output(all_parsers, capsys):
18891887
# see gh-15925
18901888
parser = all_parsers
18911889
data = "a\n1\n1,2,3\n4\n5,6,7"
@@ -1896,8 +1894,8 @@ def test_suppress_error_output(all_parsers):
18961894
warn_bad_lines=False)
18971895
tm.assert_frame_equal(result, expected)
18981896

1899-
val = sys.stderr.getvalue()
1900-
assert val == ""
1897+
captured = capsys.readouterr()
1898+
assert captured.err == ""
19011899

19021900

19031901
def test_read_table_deprecated(all_parsers):

Diff for: pandas/tests/io/parser/test_python_parser_only.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"""
99

1010
import csv
11-
import sys
1211

1312
import pytest
1413

@@ -248,8 +247,7 @@ def fail_read():
248247
fail_read()
249248

250249

251-
@tm.capture_stderr
252-
def test_none_delimiter(python_parser_only):
250+
def test_none_delimiter(python_parser_only, capsys):
253251
# see gh-13374 and gh-17465
254252
parser = python_parser_only
255253
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):
263261
error_bad_lines=False)
264262
tm.assert_frame_equal(result, expected)
265263

266-
warning = sys.stderr.getvalue()
267-
assert "Skipping line 3" in warning
264+
captured = capsys.readouterr()
265+
assert "Skipping line 3" in captured.err
268266

269267

270268
@pytest.mark.parametrize("data", [

Diff for: pandas/tests/io/parser/test_textreader.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"""
77

88
import os
9-
import sys
109

1110
import numpy as np
1211
from numpy import nan
@@ -135,8 +134,7 @@ def test_integer_thousands_alt(self):
135134
expected = DataFrame([123456, 12500])
136135
tm.assert_frame_equal(result, expected)
137136

138-
@tm.capture_stderr
139-
def test_skip_bad_lines(self):
137+
def test_skip_bad_lines(self, capsys):
140138
# too many lines, see #2430 for why
141139
data = ('a:b:c\n'
142140
'd:e:f\n'
@@ -164,10 +162,10 @@ def test_skip_bad_lines(self):
164162
error_bad_lines=False,
165163
warn_bad_lines=True)
166164
reader.read()
167-
val = sys.stderr.getvalue()
165+
captured = capsys.readouterr()
168166

169-
assert 'Skipping line 4' in val
170-
assert 'Skipping line 6' in val
167+
assert 'Skipping line 4' in captured.err
168+
assert 'Skipping line 6' in captured.err
171169

172170
def test_header_not_enough_lines(self):
173171
data = ('skip this\n'

Diff for: pandas/tests/series/test_repr.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# pylint: disable-msg=E1101,W0612
33

44
from datetime import datetime, timedelta
5-
import sys
65

76
import numpy as np
87

@@ -121,15 +120,14 @@ def test_tidy_repr(self):
121120
a.name = 'title1'
122121
repr(a) # should not raise exception
123122

124-
@tm.capture_stderr
125-
def test_repr_bool_fails(self):
123+
def test_repr_bool_fails(self, capsys):
126124
s = Series([DataFrame(np.random.randn(2, 2)) for i in range(5)])
127125

128126
# It works (with no Cython exception barf)!
129127
repr(s)
130128

131-
output = sys.stderr.getvalue()
132-
assert output == ''
129+
captured = capsys.readouterr()
130+
assert captured.err == ''
133131

134132
def test_repr_name_iterable_indexable(self):
135133
s = Series([1, 2, 3], name=np.int64(3))

Diff for: pandas/util/testing.py

-46
Original file line numberDiff line numberDiff line change
@@ -684,52 +684,6 @@ def wrapper(*args, **kwargs):
684684
return wrapper
685685

686686

687-
def capture_stderr(f):
688-
r"""
689-
Decorator to capture stderr in a buffer so that it can be checked
690-
(or suppressed) during testing.
691-
692-
Parameters
693-
----------
694-
f : callable
695-
The test that is capturing stderr.
696-
697-
Returns
698-
-------
699-
f : callable
700-
The decorated test ``f``, which captures stderr.
701-
702-
Examples
703-
--------
704-
705-
>>> from pandas.util.testing import capture_stderr
706-
>>> import sys
707-
>>>
708-
>>> @capture_stderr
709-
... def test_stderr_pass():
710-
... sys.stderr.write("foo")
711-
... out = sys.stderr.getvalue()
712-
... assert out == "foo\n"
713-
>>>
714-
>>> @capture_stderr
715-
... def test_stderr_fail():
716-
... sys.stderr.write("foo")
717-
... out = sys.stderr.getvalue()
718-
... assert out == "bar\n"
719-
...
720-
AssertionError: assert 'foo\n' == 'bar\n'
721-
"""
722-
723-
@compat.wraps(f)
724-
def wrapper(*args, **kwargs):
725-
try:
726-
sys.stderr = StringIO()
727-
f(*args, **kwargs)
728-
finally:
729-
sys.stderr = sys.__stderr__
730-
731-
return wrapper
732-
733687
# -----------------------------------------------------------------------------
734688
# Console debugging tools
735689

Diff for: scripts/tests/test_validate_docstrings.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import textwrap
55
import pytest
66
import numpy as np
7-
from pandas.util.testing import capture_stderr
87
import validate_docstrings
98
validate_one = validate_docstrings.validate_one
109

@@ -739,36 +738,32 @@ def _import_path(self, klass=None, func=None):
739738

740739
return base_path
741740

742-
@capture_stderr
743-
def test_good_class(self):
741+
def test_good_class(self, capsys):
744742
errors = validate_one(self._import_path(
745743
klass='GoodDocStrings'))['errors']
746744
assert isinstance(errors, list)
747745
assert not errors
748746

749-
@capture_stderr
750747
@pytest.mark.parametrize("func", [
751748
'plot', 'sample', 'random_letters', 'sample_values', 'head', 'head1',
752749
'contains', 'mode', 'good_imports'])
753-
def test_good_functions(self, func):
750+
def test_good_functions(self, capsys, func):
754751
errors = validate_one(self._import_path(
755752
klass='GoodDocStrings', func=func))['errors']
756753
assert isinstance(errors, list)
757754
assert not errors
758755

759-
@capture_stderr
760-
def test_bad_class(self):
756+
def test_bad_class(self, capsys):
761757
errors = validate_one(self._import_path(
762758
klass='BadGenericDocStrings'))['errors']
763759
assert isinstance(errors, list)
764760
assert errors
765761

766-
@capture_stderr
767762
@pytest.mark.parametrize("func", [
768763
'func', 'astype', 'astype1', 'astype2', 'astype3', 'plot', 'method',
769764
'private_classes',
770765
])
771-
def test_bad_generic_functions(self, func):
766+
def test_bad_generic_functions(self, capsys, func):
772767
errors = validate_one(self._import_path( # noqa:F821
773768
klass='BadGenericDocStrings', func=func))['errors']
774769
assert isinstance(errors, list)

0 commit comments

Comments
 (0)