-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
TST: Fix assert raises in sklearn/metrics/tests/
#14715
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
Conversation
related to #14216
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as other PRs: remove assert_raise_message
.
sklearn/metrics/tests/test_common.py
Outdated
@@ -556,7 +555,7 @@ def test_not_symmetric_metric(name): | ||
metric = ALL_METRICS[name] | ||
|
||
# use context manager to supply custom error message | ||
with assert_raises(AssertionError) as cm: | ||
with pytest.raises(AssertionError) as cm: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not work as intended with pytest.
I think that we want something like this instead
with pytest.raises(AssertionError):
assert_array_equal(metric(y_true, y_pred), metric(y_pred, y_true))
raise ValueError("%s seems to be symmetric:" %name)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@glemaitre Thanks for pointing that out! Will make the required change.
sklearn/metrics/tests/test_common.py
Outdated
@@ -1140,7 +1152,7 @@ def check_sample_weight_invariance(name, metric, y1, y2): | ||
weighted_score = metric(y1, y2, sample_weight=sample_weight) | ||
|
||
# use context manager to supply custom error message | ||
with assert_raises(AssertionError) as cm: | ||
with pytest.raises(AssertionError) as cm: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same issue than above. You need to raise a ValueError within the context manager.
assert_raises_regex(ValueError, "Mean Squared Logarithmic Error cannot be " | ||
"used when targets contain negative values.", | ||
mean_squared_log_error, [1., -2., 3.], [1., 2., 3.]) | ||
with pytest.raises(ValueError, match="Mean Squared Logarithmic Error " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can move the message out of raises
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise LGTM. We should open something to refactor these tests with parametrization.
@rth. I have made the required changes. You can have a look. |
@rth I have removed all except one instances of |
@thomasjpfan Can you have a look? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @sameshl !
sklearn/metrics/tests/
sklearn/metrics/tests/
replaced
assert_raises
andassert_raises_regex
withpytest.raises
context manager.related to #14216