Skip to content

Remove the use of assert_raises and assert_raises_regex from the tests #14216

@NicolasHug

Description

@NicolasHug

(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

Some more:

  • sklearn/metrics/cluster/tests/test_unsupervised.py see TST Change function names in metrics/cluster/tests/test_unsupervised.py #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions