Skip to content

Commit 8e0bbd7

Browse files
committed
TST: Check more error messages in tests
Closes pandas-devgh-16521.
1 parent c55dbf0 commit 8e0bbd7

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed
+13-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# coding: utf-8
22

3-
import pytest
4-
3+
from datetime import datetime
54
from pandas.io.msgpack import packb, unpackb
65

6+
import pytest
7+
import pandas.util.testing as tm
8+
79

810
class DummyException(Exception):
911
pass
@@ -12,12 +14,13 @@ class DummyException(Exception):
1214
class TestExceptions(object):
1315

1416
def test_raise_on_find_unsupported_value(self):
15-
import datetime
16-
pytest.raises(TypeError, packb, datetime.datetime.now())
17+
msg = "can\'t serialize datetime"
18+
with tm.assert_raises_regex(TypeError, msg):
19+
packb(datetime.now())
1720

1821
def test_raise_from_object_hook(self):
19-
def hook(obj):
20-
raise DummyException
22+
def hook(_):
23+
raise DummyException()
2124

2225
pytest.raises(DummyException, unpackb, packb({}), object_hook=hook)
2326
pytest.raises(DummyException, unpackb, packb({'fizz': 'buzz'}),
@@ -30,5 +33,7 @@ def hook(obj):
3033
packb({'fizz': {'buzz': 'spam'}}),
3134
object_pairs_hook=hook)
3235

33-
def test_invalidvalue(self):
34-
pytest.raises(ValueError, unpackb, b'\xd9\x97#DL_')
36+
def test_invalid_value(self):
37+
msg = "Unpack failed: error"
38+
with tm.assert_raises_regex(ValueError, msg):
39+
unpackb(b"\xd9\x97#DL_")

pandas/tests/series/test_validate.py

+17-21
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1-
import pytest
21
from pandas.core.series import Series
32

3+
import pytest
4+
import pandas.util.testing as tm
5+
46

57
class TestSeriesValidate(object):
68
"""Tests for error handling related to data types of method arguments."""
7-
s = Series([1, 2, 3, 4, 5])
8-
9-
def test_validate_bool_args(self):
10-
# Tests for error handling related to boolean arguments.
11-
invalid_values = [1, "True", [1, 2, 3], 5.0]
12-
13-
for value in invalid_values:
14-
with pytest.raises(ValueError):
15-
self.s.reset_index(inplace=value)
16-
17-
with pytest.raises(ValueError):
18-
self.s._set_name(name='hello', inplace=value)
199

20-
with pytest.raises(ValueError):
21-
self.s.sort_values(inplace=value)
10+
@classmethod
11+
def setup_class(cls):
12+
cls.s = Series([1, 2, 3, 4, 5])
2213

23-
with pytest.raises(ValueError):
24-
self.s.sort_index(inplace=value)
14+
@pytest.mark.parametrize("func", ["reset_index", "_set_name",
15+
"sort_values", "sort_index",
16+
"rename", "dropna"])
17+
@pytest.mark.parametrize("inplace", [1, "True", [1, 2, 3], 5.0])
18+
def test_validate_bool_args(self, func, inplace):
19+
msg = "For argument \"inplace\" expected type bool"
20+
kwargs = dict(inplace=inplace)
2521

26-
with pytest.raises(ValueError):
27-
self.s.rename(inplace=value)
22+
if func == "_set_name":
23+
kwargs["name"] = "hello"
2824

29-
with pytest.raises(ValueError):
30-
self.s.dropna(inplace=value)
25+
with tm.assert_raises_regex(ValueError, msg):
26+
getattr(self.s, func)(**kwargs)

0 commit comments

Comments
 (0)