Skip to content

TST replace assert_raises with the pytest.raises context manager in dummy module #19372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions sklearn/tests/test_dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from sklearn.utils._testing import assert_array_equal
from sklearn.utils._testing import assert_array_almost_equal
from sklearn.utils._testing import assert_almost_equal
from sklearn.utils._testing import assert_raises
from sklearn.utils._testing import assert_warns_message
from sklearn.utils._testing import ignore_warnings
from sklearn.utils.stats import _weighted_percentile
Expand Down Expand Up @@ -257,10 +256,13 @@ def test_classifier_prediction_independent_of_X(strategy):

def test_classifier_exceptions():
clf = DummyClassifier(strategy="unknown")
assert_raises(ValueError, clf.fit, [], [])
with pytest.raises(ValueError):
clf.fit([], [])

assert_raises(NotFittedError, clf.predict, [])
assert_raises(NotFittedError, clf.predict_proba, [])
with pytest.raises(NotFittedError):
clf.predict([])
with pytest.raises(NotFittedError):
clf.predict_proba([])


def test_mean_strategy_regressor():
Expand Down Expand Up @@ -299,7 +301,8 @@ def test_mean_strategy_multioutput_regressor():

def test_regressor_exceptions():
reg = DummyRegressor()
assert_raises(NotFittedError, reg.predict, [])
with pytest.raises(NotFittedError):
reg.predict([])


def test_median_strategy_regressor():
Expand Down Expand Up @@ -401,27 +404,34 @@ def test_quantile_invalid():
y = [0] * 5 # ignored

est = DummyRegressor(strategy="quantile")
assert_raises(ValueError, est.fit, X, y)
with pytest.raises(ValueError):
est.fit(X, y)

est = DummyRegressor(strategy="quantile", quantile=None)
assert_raises(ValueError, est.fit, X, y)
with pytest.raises(ValueError):
est.fit(X, y)

est = DummyRegressor(strategy="quantile", quantile=[0])
assert_raises(ValueError, est.fit, X, y)
with pytest.raises(ValueError):
est.fit(X, y)

est = DummyRegressor(strategy="quantile", quantile=-0.1)
assert_raises(ValueError, est.fit, X, y)
with pytest.raises(ValueError):
est.fit(X, y)

est = DummyRegressor(strategy="quantile", quantile=1.1)
assert_raises(ValueError, est.fit, X, y)
with pytest.raises(ValueError):
est.fit(X, y)

est = DummyRegressor(strategy="quantile", quantile='abc')
assert_raises(TypeError, est.fit, X, y)
with pytest.raises(TypeError):
est.fit(X, y)


def test_quantile_strategy_empty_train():
est = DummyRegressor(strategy="quantile", quantile=0.4)
assert_raises(ValueError, est.fit, [], [])
with pytest.raises(ValueError):
est.fit([], [])


def test_constant_strategy_regressor():
Expand Down Expand Up @@ -479,15 +489,17 @@ def test_unknown_strategey_regressor():
y = [1, 2, 4, 6, 8]

est = DummyRegressor(strategy='gona')
assert_raises(ValueError, est.fit, X, y)
with pytest.raises(ValueError):
est.fit(X, y)


def test_constants_not_specified_regressor():
X = [[0]] * 5
y = [1, 2, 4, 6, 8]

est = DummyRegressor(strategy='constant')
assert_raises(TypeError, est.fit, X, y)
with pytest.raises(TypeError):
est.fit(X, y)


def test_constant_size_multioutput_regressor():
Expand All @@ -496,7 +508,8 @@ def test_constant_size_multioutput_regressor():
y = random_state.randn(10, 5)

est = DummyRegressor(strategy='constant', constant=[1, 2, 3, 4])
assert_raises(ValueError, est.fit, X, y)
with pytest.raises(ValueError):
est.fit(X, y)


def test_constant_strategy():
Expand Down