diff --git a/pandas/tests/test_testing.py b/pandas/tests/test_testing.py index 19598a54c6585..caacfc4115b68 100644 --- a/pandas/tests/test_testing.py +++ b/pandas/tests/test_testing.py @@ -627,6 +627,21 @@ def test_frame_equal_message(self): by_blocks=True) +class TestIsInstance(tm.TestCase): + + def test_isinstance(self): + + expected = "Expected type " + with assertRaisesRegexp(AssertionError, expected): + tm.assertIsInstance(1, pd.Series) + + def test_notisinstance(self): + + expected = "Input must not be type " + with assertRaisesRegexp(AssertionError, expected): + tm.assertNotIsInstance(pd.Series([1]), pd.Series) + + class TestRNGContext(unittest.TestCase): def test_RNGContext(self): diff --git a/pandas/util/testing.py b/pandas/util/testing.py index f0406690a588c..eb1d5c6c2d0d0 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -868,9 +868,9 @@ def assertIsInstance(obj, cls, msg=''): """Test that obj is an instance of cls (which can be a class or a tuple of classes, as supported by isinstance()).""" - assert isinstance(obj, cls), ( - "%sExpected object to be of type %r, found %r instead" % ( - msg, cls, type(obj))) + if not isinstance(obj, cls): + err_msg = "{0}Expected type {1}, found {2} instead" + raise AssertionError(err_msg.format(msg, cls, type(obj))) def assert_isinstance(obj, class_type_or_tuple, msg=''): @@ -882,9 +882,9 @@ def assertNotIsInstance(obj, cls, msg=''): """Test that obj is not an instance of cls (which can be a class or a tuple of classes, as supported by isinstance()).""" - assert not isinstance(obj, cls), ( - "%sExpected object to be of type %r, found %r instead" % ( - msg, cls, type(obj))) + if isinstance(obj, cls): + err_msg = "{0}Input must not be type {1}" + raise AssertionError(err_msg.format(msg, cls)) def assert_categorical_equal(res, exp):