Skip to content

Commit 78ec1ca

Browse files
committed
MNT: Use pytest-catchlog to handle testing log messages.
Pytest 3.1.0 broke our current use of capfd, but that doesn't seem to be a good way to handle capturing of log messages. (pytest-dev/pytest#2079) So instead use pytest-catchlog, which was recommended in that issue and seems to be maintained.
1 parent 069faa0 commit 78ec1ca

File tree

4 files changed

+20
-46
lines changed

4 files changed

+20
-46
lines changed

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
extras_require={
4646
'netcdf': 'netCDF4>=1.1.0',
4747
'dev': 'ipython[all]>=3.1',
48-
'test': ['pytest', 'pytest-flake8', 'pytest-runner', 'netCDF4>=1.1.0',
48+
'test': ['pytest', 'pytest-catchlog', 'pytest-flake8', 'pytest-runner',
49+
'netCDF4>=1.1.0',
4950
'flake8>3.2.0', 'flake8-builtins', 'flake8-comprehensions',
5051
'flake8-copyright', 'flake8-import-order', 'flake8-mutable',
5152
'flake8-pep3101', 'flake8-print', 'flake8-quotes',

siphon/cdmr/tests/test_ncstream.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,10 @@ def test_local_data():
6262
assert messages[0][0] == '2014-10-28T21:00:00Z'
6363

6464

65-
def test_bad_magic():
65+
def test_bad_magic(caplog):
6666
"""Test that we get notified of bad magic bytes in stream."""
67-
import logging
68-
import sys
69-
70-
# Only StringIO's version supports writing str
71-
if sys.version_info.major == 2:
72-
from StringIO import StringIO
73-
else:
74-
from io import StringIO
75-
76-
# Set up capturing of logging
77-
log = logging.getLogger('siphon.cdmr.ncstream')
78-
err_out = StringIO()
79-
log.addHandler(logging.StreamHandler(err_out))
80-
8167
# Try reading a bad message
8268
f = BytesIO(b'\x00\x01\x02\x03')
8369
read_ncstream_messages(f)
8470

85-
log.handlers.pop()
86-
87-
# Make sure we got some error output
88-
err_out.seek(0)
89-
assert 'Unknown magic' in err_out.read()
71+
assert 'Unknown magic' in caplog.text

siphon/tests/test_metadata.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,13 +545,12 @@ def test_data_format(self):
545545
assert 'dataFormat' in md
546546
assert md['dataFormat'] == 'GRIB-1'
547547

548-
def test_data_malformed_format(self, capfd):
548+
def test_data_malformed_format(self, caplog):
549549
"""Test getting a warning for a malformed dataFormat tag."""
550550
xml = '<dataFormat>netCDF-4</dataFormat>'
551551
element = self._make_element(xml)
552552
TDSCatalogMetadata(element)
553-
out, err = capfd.readouterr()
554-
assert 'Value netCDF-4 not valid for type dataFormat' in ''.join(err)
553+
assert 'Value netCDF-4 not valid for type dataFormat' in caplog.text
555554

556555
def test_data_type(self):
557556
"""Test parsing dataType tags."""

siphon/tests/test_ncss_dataset.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,13 @@ def test_attribute_byte(self):
5555
actual = self.types.handle_attribute(element)
5656
assert expected == actual
5757

58-
def test_attribute_invalid_byte(self, capfd):
58+
def test_attribute_invalid_byte(self, caplog):
5959
"""Test parsing an invalid byte attribute."""
6060
xml = '<attribute name="missing_value" type="byte" value="a"/>'
6161
element = ET.fromstring(xml)
6262
expected = {'missing_value': ['a']}
6363
actual = self.types.handle_attribute(element)
64-
expected_message = 'Cannot convert [\'a\'] to int. Keeping type as str.'
65-
assert expected_message in ''.join(capfd.readouterr())
64+
assert 'Cannot convert [\'a\'] to int. Keeping type as str.' in caplog.text
6665
assert expected == actual
6766

6867
def test_attribute_short(self):
@@ -73,14 +72,13 @@ def test_attribute_short(self):
7372
actual = self.types.handle_attribute(element)
7473
assert expected == actual
7574

76-
def test_attribute_invalid_short(self, capfd):
75+
def test_attribute_invalid_short(self, caplog):
7776
"""Test parsing an invalid short attribute."""
7877
xml = '<attribute name="missing_value" type="short" value="a"/>'
7978
element = ET.fromstring(xml)
8079
expected = {'missing_value': ['a']}
8180
actual = self.types.handle_attribute(element)
82-
expected_message = 'Cannot convert [\'a\'] to int. Keeping type as str.'
83-
assert expected_message in ''.join(capfd.readouterr())
81+
assert 'Cannot convert [\'a\'] to int. Keeping type as str.' in caplog.text
8482
assert expected == actual
8583

8684
def test_attribute_int(self):
@@ -91,14 +89,13 @@ def test_attribute_int(self):
9189
actual = self.types.handle_attribute(element)
9290
assert expected == actual
9391

94-
def test_attribute_invalid_int(self, capfd):
92+
def test_attribute_invalid_int(self, caplog):
9593
"""Test parsing an invalid int attribute."""
9694
xml = '<attribute name="missing_value" type="int" value="a"/>'
9795
element = ET.fromstring(xml)
9896
expected = {'missing_value': ['a']}
9997
actual = self.types.handle_attribute(element)
100-
expected_message = 'Cannot convert [\'a\'] to int. Keeping type as str.'
101-
assert expected_message in ''.join(capfd.readouterr())
98+
assert 'Cannot convert [\'a\'] to int. Keeping type as str.' in caplog.text
10299
assert expected == actual
103100

104101
def test_attribute_long(self):
@@ -109,14 +106,13 @@ def test_attribute_long(self):
109106
actual = self.types.handle_attribute(element)
110107
assert expected == actual
111108

112-
def test_attribute_invalid_long(self, capfd):
109+
def test_attribute_invalid_long(self, caplog):
113110
"""Test parsing a invalid long attribute."""
114111
xml = '<attribute name="missing_value" type="long" value="a"/>'
115112
element = ET.fromstring(xml)
116113
expected = {'missing_value': ['a']}
117114
actual = self.types.handle_attribute(element)
118-
expected_message = 'Cannot convert [\'a\'] to int. Keeping type as str.'
119-
assert expected_message in ''.join(capfd.readouterr())
115+
assert 'Cannot convert [\'a\'] to int. Keeping type as str.' in caplog.text
120116
assert expected == actual
121117

122118
def test_attribute_float(self):
@@ -127,14 +123,13 @@ def test_attribute_float(self):
127123
actual = self.types.handle_attribute(element)
128124
assert expected == actual
129125

130-
def test_attribute_invalid_float(self, capfd):
126+
def test_attribute_invalid_float(self, caplog):
131127
"""Test parsing an invalid float value attribute."""
132128
xml = '<attribute name="missing_value" type="float" value="a"/>'
133129
element = ET.fromstring(xml)
134130
expected = {'missing_value': ['a']}
135131
actual = self.types.handle_attribute(element)
136-
expected_message = 'Cannot convert [\'a\'] to float. Keeping type as str.'
137-
assert expected_message in ''.join(capfd.readouterr())
132+
assert 'Cannot convert [\'a\'] to float. Keeping type as str.' in caplog.text
138133
assert expected == actual
139134

140135
def test_attribute_float_nan(self):
@@ -156,14 +151,13 @@ def test_attribute_double(self):
156151
actual = self.types.handle_attribute(element)
157152
assert expected == actual
158153

159-
def test_attribute_invalid_double(self, capfd):
154+
def test_attribute_invalid_double(self, caplog):
160155
"""Test parsing an invalid double attribute."""
161156
xml = '<attribute name="missing_value" type="double" value="a"/>'
162157
element = ET.fromstring(xml)
163158
expected = {'missing_value': ['a']}
164159
actual = self.types.handle_attribute(element)
165-
expected_message = 'Cannot convert [\'a\'] to float. Keeping type as str.'
166-
assert expected_message in ''.join(capfd.readouterr())
160+
assert 'Cannot convert [\'a\'] to float. Keeping type as str.' in caplog.text
167161
assert expected == actual
168162

169163
def test_attribute_double_nan(self):
@@ -210,15 +204,13 @@ def test_attribute_boolean_false(self):
210204
actual = self.types.handle_attribute(element)
211205
assert expected == actual
212206

213-
def test_attribute_boolean_invalid(self, capfd):
207+
def test_attribute_boolean_invalid(self, caplog):
214208
"""Test parsing an invalid boolean attribute."""
215209
xml = '<attribute name="missing_value" type="boolean" value="a"/>'
216210
element = ET.fromstring(xml)
217211
expected = {'missing_value': ['a']}
218212
actual = self.types.handle_attribute(element)
219-
expected_message = 'Cannot convert values [\'a\'] to boolean.'
220-
expected_message += ' Keeping type as str.'
221-
assert expected_message in ''.join(capfd.readouterr())
213+
assert 'Cannot convert values [\'a\'] to boolean. Keeping type as str.' in caplog.text
222214
assert expected == actual
223215

224216
def test_value_1(self):

0 commit comments

Comments
 (0)