From 9d069bab6eb03ccb634e6de1ae424d4adbd76a3b Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Fri, 14 Sep 2018 00:05:03 +0200 Subject: [PATCH] TST Use pytest.raises instead of legacy constructs (#22681) --- pandas/tests/frame/test_indexing.py | 6 +----- pandas/tests/plotting/test_frame.py | 6 +----- pandas/tests/series/test_combine_concat.py | 2 +- pandas/tests/series/test_constructors.py | 2 +- pandas/tests/test_config.py | 8 +++----- 5 files changed, 7 insertions(+), 17 deletions(-) diff --git a/pandas/tests/frame/test_indexing.py b/pandas/tests/frame/test_indexing.py index f0c4d7be2f2935..96b2e98dd7e8d4 100644 --- a/pandas/tests/frame/test_indexing.py +++ b/pandas/tests/frame/test_indexing.py @@ -71,12 +71,8 @@ def test_getitem(self): def test_getitem_dupe_cols(self): df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=['a', 'a', 'b']) - try: + with pytest.raises(KeyError): df[['baf']] - except KeyError: - pass - else: - self.fail("Dataframe failed to raise KeyError") def test_get(self): b = self.frame.get('B') diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 47a93ba82d77be..772989231e9a74 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -588,17 +588,13 @@ def test_subplots_layout(self): @pytest.mark.slow def test_subplots_warnings(self): # GH 9464 - warnings.simplefilter('error') - try: + with tm.assert_produces_warning(None): df = DataFrame(np.random.randn(100, 4)) df.plot(subplots=True, layout=(3, 2)) df = DataFrame(np.random.randn(100, 4), index=date_range('1/1/2000', periods=100)) df.plot(subplots=True, layout=(3, 2)) - except Warning as w: - self.fail(w) - warnings.simplefilter('default') @pytest.mark.slow def test_subplots_multiple_axes(self): diff --git a/pandas/tests/series/test_combine_concat.py b/pandas/tests/series/test_combine_concat.py index f8420b302836e5..35ba4fbf0ce25f 100644 --- a/pandas/tests/series/test_combine_concat.py +++ b/pandas/tests/series/test_combine_concat.py @@ -28,7 +28,7 @@ def test_append(self): elif idx in self.objSeries.index: assert value == self.objSeries[idx] else: - self.fail("orphaned index!") + raise AssertionError("orphaned index!") pytest.raises(ValueError, self.ts.append, self.ts, verify_integrity=True) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index d2fbd69a2a08f8..9faf47ace242d9 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -465,7 +465,7 @@ def test_constructor_index_mismatch(self, input): # test that construction of a Series with an index of different length # raises an error msg = 'Length of passed values is 3, index implies 4' - with pytest.raises(ValueError, message=msg): + with pytest.raises(ValueError, match=msg): Series(input, index=np.arange(4)) def test_constructor_numpy_scalar(self): diff --git a/pandas/tests/test_config.py b/pandas/tests/test_config.py index 91ce65dcce9b2e..fd8e98c483f78a 100644 --- a/pandas/tests/test_config.py +++ b/pandas/tests/test_config.py @@ -247,12 +247,10 @@ def test_deprecate_option(self): assert self.cf._is_deprecated('foo') with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') - try: + with pytest.raises( + KeyError, + message="Nonexistent option didn't raise KeyError"): self.cf.get_option('foo') - except KeyError: - pass - else: - self.fail("Nonexistent option didn't raise KeyError") assert len(w) == 1 # should have raised one warning assert 'deprecated' in str(w[-1]) # we get the default message