Skip to content

Unseeded random sizes in test_image_processing.py make coverage nondeterministic #638

Description

@aymuos15

test_crop_center_2d and test_pad_centre_2d in tests/core/test_unit/test_tools/test_image_processing.py choose crop/pad sizes with unseeded random.randint:

def test_crop_center_2d():
    x_shape = random.randint(2, 100)
    ...
    new_x_shape = random.randint(1, x_shape)

def test_pad_centre_2d():
    ...
    new_x_shape = random.randint(x_shape, x_shape * 10)

crop_center_2d / pad_center_2d branch on is_even(pad), so whether the even-branch or odd-branch line executes depends on those random sizes. As a result, cellfinder/core/tools/image_processing.py reports a slightly different set of covered lines on each run, with no code change.

Reproduce:

pytest tests/core/test_unit/test_tools/test_image_processing.py \
  --cov=cellfinder.core.tools.image_processing --cov-report=term-missing -p no:xdist

Running it a few times, the missing lines flip between e.g. 77-78, 84 (11 missed) and 77-78, 86-87 (12 missed). Adding random.seed(0); np.random.seed(0) to the two tests makes the missing lines identical on every run.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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