Remove the use of assert_raises and assert_raises_regex from the tests
See original GitHub issue~(Saving this for the upcoming sprints, ideally)~
Let’s remove the use of assert_raises, assert_raise_message, and assert_raises_regex.
These should be replaced with the pytest context manager:
with pytest.raises(TheException, match="the expected message"):
function_call_here()
(no need for match in the case of assert_raises and assert_raise_message I guess).
For contributors: pick one of the modules below, and please comment on this issue saying e.g. “I’m working on cluster/tests”, to avoid other contributors choosing the same modules.
You can see all the occurrences of the entries that need to be removed with e.g. git grep "assert_raises" sklearn/ensemble/tests/.
Modules that need cleaning
-
sklearn/cluster/tests/#14649) -
sklearn/compose/tests/#14670 -
sklearn/covariance/tests/#14674 -
sklearn/datasets/tests/#14676 -
sklearn/decomposition/tests/#14679 -
sklearn/ensemble/tests/#19399 -
sklearn/feature_extraction/tests/#14694 -
sklearn/feature_selection/tests/#14697 -
sklearn/linear_model/tests/#19440 -
sklearn/manifold/tests/#14699 -
sklearn/metrics/cluster/tests/#14707 -
sklearn/metrics/tests/#14715 -
sklearn/model_selection/tests/#19592 -
sklearn/neighbors/tests/#19388 -
sklearn/neural_network/tests/#14716 -
sklearn/preprocessing/tests/#14717 -
sklearn/semi_supervised/tests/#14841 -
sklearn/svm/tests/#14727 -
sklearn/tests/#19500 -
sklearn/tree/tests/#14737 -
sklearn/utils/estimator_checks.py -
sklearn/utils/tests/#16337
Some more:
-
see #20065sklearn/metrics/cluster/tests/test_unsupervised.py -
sklearn/compose/tests/test_column_transformer.py -
sklearn/covariance/tests/test_robust_covariance.py -
sklearn/datasets/tests/test_openml.py -
sklearn/datasets/tests/test_samples_generator.py -
sklearn/decomposition/tests/test_nmf.py -
sklearn/decomposition/tests/test_factor_analysis.py -
sklearn/feature_extraction/tests/test_text.py -
sklearn/linear_model/tests/test_bayes.py -
sklearn/linear_model/tests/test_sag.py -
sklearn/linear_model/tests/test_ransac.py -
sklearn/manifold/tests/test_locally_linear.py -
sklearn/metrics/cluster/tests/test_unsupervised.py -
sklearn/mixture/tests/test_bayesian_mixture.py -
sklearn/mixture/tests/test_gaussian_mixture.py -
sklearn/svm/tests/test_bounds.py -
sklearn/svm/tests/test_sparse.py -
sklearn/svm/tests/test_svm.py -
sklearn/tests/test_base.py -
sklearn/tests/test_isotonic.py
Also:
-
sklearn/utils/tests/test_estimator_checks.pybut this should use the customraisesCM insklearn/utils/_testing.pyinstead, as we don’t want to use pytest for this file
Issue Analytics
- State:
- Created 4 years ago
- Comments:52 (52 by maintainers)
Top Results From Across the Web
unittest — Unit testing framework — Python 3.11.1 ...
It checks for a specific response to a particular set of inputs. unittest provides a base class, TestCase , which may be used...
Read more >How do you test that a Python function throws an exception?
Use TestCase.assertRaises (or TestCase.failUnlessRaises ) from the unittest module, for example: import mymod class MyTestCase(unittest.
Read more >Replace `unittest.TestCase.assertRaises` with ... - GitHub
assertRaises to test an exception is raised for illegal operations, but they need to be replaces with unittest.TestCase.assertRaisesRegex.
Read more >T154281 [recurring] Replace assertRaises with ...
assertRaisesRegex has an extra parameter to check the exception message, and should be used instead. By checking the exception message, the unit test...
Read more >How to write and report assertions in tests - Pytest
pytest allows you to use the standard Python assert for verifying expectations and values in Python tests. For example, you can write the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I also don’t find any left overs. As discussed, the exception is
sklearn/utils/estimator_checks.py(and dependendies) where we do not want to depend on pytest.A Great Thank You to all contributors!
@NicolasHug the test_estimator_checks are also cleaned.