Skip to content

Use modifiable global random state in tests #13913

@rth

Description

@rth

As mentioned by @jnothman in #13846 (comment)

Relatedly, I proposed having a random_seed fixture that was globally set to different values on different testing runs. One benefit would be that we could easily distinguish those tests that are invariant under changing random seed from those that are brittle.

I think it would be a good idea. For instance, we could,

  • create a global auto-use pytest fixture in scikit-learn/conftest.py,
    @pytest.fixture(scope="session")
    def pytest_rng():
        random_seed = os.environ.get('SKLEARN_TEST_RNG', 42)
        return np.random.RandomState(random_seed)
  • modify tests to use it, e.g.
    -  def test_something():
    +  def test_something(pytest_rng):
    -     rng = np.random.RandomState(0)
    -     est = Estimator(random_state=rng)
    +     est = Estimator(random_state=pytest_rng)

One issue is that global auto-use fixtures are a bit magical, but I'm hoping that naming it as pytest_rng it would be explicit enough.

Edit: updated to avoid using an auto-use fixture.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions