Skip to content

TST replace assert_raise_* by pytest.raises in tests/test_multioutput.py #19618

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
Mar 8, 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
36 changes: 23 additions & 13 deletions sklearn/tests/test_multioutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
from joblib import cpu_count

from sklearn.utils._testing import assert_almost_equal
from sklearn.utils._testing import assert_raises
from sklearn.utils._testing import assert_raises_regex
from sklearn.utils._testing import assert_raise_message
from sklearn.utils._testing import assert_array_equal
from sklearn.utils._testing import assert_array_almost_equal
from sklearn import datasets
Expand Down Expand Up @@ -80,7 +77,9 @@ def test_multi_target_regression_one_target():
# Test multi target regression raises
X, y = datasets.make_regression(n_targets=1)
rgr = MultiOutputRegressor(GradientBoostingRegressor(random_state=0))
assert_raises(ValueError, rgr.fit, X, y)
msg = 'at least two dimensions'
with pytest.raises(ValueError, match=msg):
rgr.fit(X, y)


def test_multi_target_sparse_regression():
Expand All @@ -106,8 +105,9 @@ def test_multi_target_sample_weights_api():
w = [0.8, 0.6]

rgr = MultiOutputRegressor(OrthogonalMatchingPursuit())
assert_raises_regex(ValueError, "does not support sample weights",
rgr.fit, X, y, w)
msg = "does not support sample weights"
with pytest.raises(ValueError, match=msg):
rgr.fit(X, y, w)

# no exception should be raised if the base estimator supports weights
rgr = MultiOutputRegressor(GradientBoostingRegressor(random_state=0))
Expand Down Expand Up @@ -252,9 +252,9 @@ def test_multi_output_classification_partial_fit():
def test_multi_output_classification_partial_fit_no_first_classes_exception():
sgd_linear_clf = SGDClassifier(loss='log', random_state=1, max_iter=5)
multi_target_linear = MultiOutputClassifier(sgd_linear_clf)
assert_raises_regex(ValueError, "classes must be passed on the first call "
"to partial_fit.",
multi_target_linear.partial_fit, X, y)
msg = "classes must be passed on the first call to partial_fit."
with pytest.raises(ValueError, match=msg):
multi_target_linear.partial_fit(X, y)


def test_multi_output_classification():
Expand Down Expand Up @@ -386,17 +386,27 @@ def test_multi_output_exceptions():
# NotFittedError when fit is not done but score, predict and
# and predict_proba are called
moc = MultiOutputClassifier(LinearSVC(random_state=0))
assert_raises(NotFittedError, moc.predict, y)

with pytest.raises(NotFittedError):
moc.predict(y)

with pytest.raises(NotFittedError):
moc.predict_proba
assert_raises(NotFittedError, moc.score, X, y)

with pytest.raises(NotFittedError):
moc.score(X, y)

# ValueError when number of outputs is different
# for fit and score
y_new = np.column_stack((y1, y2))
moc.fit(X, y)
assert_raises(ValueError, moc.score, X, y_new)
with pytest.raises(ValueError):
moc.score(X, y_new)

# ValueError when y is continuous
assert_raise_message(ValueError, "Unknown label type", moc.fit, X, X[:, 1])
msg = "Unknown label type"
with pytest.raises(ValueError, match=msg):
moc.fit(X, X[:, 1])


def generate_multilabel_dataset_with_correlations():
Expand Down