Implementation:Scikit learn Scikit learn TestingUtils
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Testing |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete utility module for testing helpers and assertion functions provided by scikit-learn.
Description
The _testing module provides a comprehensive collection of testing utilities for scikit-learn. It includes assertion functions like assert_allclose, context managers like ignore_warnings, and re-exports from NumPy testing such as assert_array_equal and assert_array_almost_equal. The module also provides utilities for running Python scripts and validating estimator behavior.
Usage
Use these utilities when writing unit tests for scikit-learn estimators and functions. The module provides standardized assertion functions and warning management tools that account for scikit-learn-specific edge cases.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/utils/_testing.py
Signature
def ignore_warnings(obj=None, category=Warning):
...
def assert_allclose(actual, desired, rtol=None, atol=0., ...):
...
def assert_run_python_script_without_output(source_code, timeout=60):
...
# Re-exported from numpy.testing:
# assert_almost_equal, assert_array_almost_equal, assert_array_equal, assert_array_less
Import
from sklearn.utils._testing import assert_allclose, ignore_warnings
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| actual | array-like | Yes | Actual values to check |
| desired | array-like | Yes | Expected values to compare against |
| rtol | float | No | Relative tolerance for comparison |
| atol | float | No | Absolute tolerance for comparison |
| category | Warning class | No | Warning category to ignore |
Outputs
| Name | Type | Description |
|---|---|---|
| None | None | Raises AssertionError if values do not match within tolerance |
Usage Examples
Basic Usage
import numpy as np
from sklearn.utils._testing import assert_allclose, ignore_warnings
# Assert arrays are close
actual = np.array([1.0, 2.0, 3.0])
desired = np.array([1.001, 2.001, 3.001])
assert_allclose(actual, desired, atol=0.01)
# Suppress warnings in a test function
@ignore_warnings(category=DeprecationWarning)
def test_something():
pass