scikit-learn: Remove the use of assert_raises and assert_raises_regex from the tests

~(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:

  • sklearn/metrics/cluster/tests/test_unsupervised.py see #20065
  • 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.py but this should use the custom raises CM in sklearn/utils/_testing.py instead, as we don’t want to use pytest for this file

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 52 (52 by maintainers)

Most upvoted comments

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.

@NicolasHug The following files have been cleaned on #20104:

  • 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

Based on the PR from #20065, test_unsupervised can also be marked as done because only the function names have assert_raises.

I think we can just get these done now. I upadted the list with some more stuff

git grep assert_raises still finds occurences (excluding estimator_checks). Same for assert_raises_regexp and assert_raise_message. I guess some PRs pretended to clean a whole module while only cleaning a subset.

Yes, you can replace them as well. Though if the resulting diff is already large, better to do it in a separate PR.

Me and @abdulelahsm are working on sklearn/linear_model/tests/

Their docs say:

Some Nose functions can be handled via a global search-replace, so a fixer was not a necessity:

assert_raises: replace with pytest.raises
assert_warns: replace with pytest.warns

And

Some Nose functions don't have a one-line assert statement equivalent, they have to remain utility functions:

assert_raises_regex
assert_raises_regexp # deprecated by Nose

but I’m pretty sure these can be done with a regex.

Yeah nose2pytest doesn’t support assert_raises, from the readme,

Some Nose functions can be handled via a global search-replace, so a fixer was not a necessity:

assert_raises: replace with pytest.raises

and

Some Nose functions don’t have a one-line assert statement equivalent, they have to remain utility functions:

assert_raises_regex

But it still should be possible to do it with a regex I think?