Skip to content

Commit 57916b7

Browse files
Use assert_raises_regexp to check error messages
1 parent 1a887dd commit 57916b7

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

sklearn/metrics/tests/test_score_objects.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from sklearn.utils.testing import assert_almost_equal
66
from sklearn.utils.testing import assert_raises
7-
from sklearn.utils.testing import assert_raise_message
7+
from sklearn.utils.testing import assert_raises_regexp
88
from sklearn.utils.testing import assert_true
99
from sklearn.utils.testing import ignore_warnings
1010

@@ -58,7 +58,9 @@ def predict(self, X):
5858
def test_check_scoring():
5959
"""Test all branches of check_scoring"""
6060
estimator = EstimatorWithoutFit()
61-
assert_raise_message(TypeError, "'fit' method", check_scoring, estimator)
61+
assert_raises_regexp(TypeError,
62+
r"estimator should a be an estimator implementing 'fit' method, .* "
63+
"was passed", check_scoring, estimator)
6264

6365
estimator = EstimatorWithFitAndScore()
6466
estimator.fit([[1]], [1])
@@ -67,14 +69,17 @@ def test_check_scoring():
6769

6870
estimator = EstimatorWithFitAndPredict()
6971
estimator.fit([[1]], [1])
70-
assert_raise_message(TypeError, "no scoring", check_scoring, estimator)
72+
assert_raises_regexp(TypeError,
73+
r"If no scoring is specified, the estimator passed should have a "
74+
"'score' method\. The estimator .* does not.", check_scoring, estimator)
7175

7276
scorer = check_scoring(estimator, "accuracy")
7377
assert_almost_equal(scorer(estimator, [[1]], [1]), 1.0)
7478

7579
estimator = EstimatorWithFit()
76-
assert_raise_message(TypeError, "'score' or a 'predict'", check_scoring,
77-
estimator, "accuracy")
80+
assert_raises_regexp(TypeError,
81+
r"The estimator passed should have a 'score' or a 'predict' method. "
82+
"The estimator .* does not.", check_scoring, estimator, "accuracy")
7883

7984
estimator = EstimatorWithFit()
8085
scorer = check_scoring(estimator, allow_none=True)

sklearn/utils/testing.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from nose.tools import assert_true
3535
from nose.tools import assert_false
3636
from nose.tools import assert_raises
37+
from nose.tools import assert_raises_regexp
3738
from nose.tools import raises
3839
from nose import SkipTest
3940
from nose import with_setup
@@ -47,10 +48,10 @@
4748
from sklearn.base import (ClassifierMixin, RegressorMixin, TransformerMixin,
4849
ClusterMixin)
4950

50-
__all__ = ["assert_equal", "assert_not_equal", "assert_raises", "raises",
51-
"with_setup", "assert_true", "assert_false", "assert_almost_equal",
52-
"assert_array_equal", "assert_array_almost_equal",
53-
"assert_array_less"]
51+
__all__ = ["assert_equal", "assert_not_equal", "assert_raises",
52+
"assert_raises_regexp", "raises", "with_setup", "assert_true",
53+
"assert_false", "assert_almost_equal", "assert_array_equal",
54+
"assert_array_almost_equal", "assert_array_less"]
5455

5556

5657
try:

0 commit comments

Comments
 (0)