Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add the option --randomly-seed-per-test to use a different seed for each test #617

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

brycedrennan
Copy link

@brycedrennan brycedrennan commented Apr 10, 2024

Resolves #600

I'm tempted to make it the default behavior but this is safer.

@brycedrennan brycedrennan force-pushed the bd/unique-seed-per-test branch 2 times, most recently from f61a6ea to cdd56f0 Compare April 10, 2024 00:29
Copy link
Member

@adamchainz adamchainz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this.

I think we can make it the default behaviour, no option. Otherwise it’s just another thing other users have to learn about and consider activating.

CHANGELOG.rst Outdated Show resolved Hide resolved
@brycedrennan brycedrennan force-pushed the bd/unique-seed-per-test branch 2 times, most recently from f4c777a to 946b247 Compare April 10, 2024 16:13
@brycedrennan
Copy link
Author

It's done but there is an inconsistency with how seeding works with faker. I'm not sure what would be least surprising for someone given the docs from faker.

assert fake.name() == 'Justin Richard'
def test_two():
assert fake.name() == 'Tiffany Williams'
"""
)
out = ourtester.runpytest("--randomly-seed=1")
out.assert_outcomes(passed=2)
def test_faker_fixture(ourtester):
ourtester.makepyfile(
test_one="""
def test_one(faker):
assert faker.name() == 'Ryan Gallagher'
def test_two(faker):
assert faker.name() == 'Ryan Gallagher'

https://faker.readthedocs.io/en/master/pytest-fixtures.html#seeding-configuration

@alexandrupatrascu
Copy link

hi folks, any chance this gets merged and released soon?
reason why I'm asking is that I'm running pytest-rerunfailures alongside pytest-randomly and the re-runs use the same seed ID, causing the tests to re-use the same test data and not generate new inputs, hence they keep on failing.

thank you for the help!

@brycedrennan
Copy link
Author

Sounds like you should change your test to either work with the same seed of data or change the data generator to not generate invalid data? I don't believe the changes proposed here would even help with your situation either way.

@tamird
Copy link

tamird commented May 31, 2024

This would be very useful indeed.

In the meantime I worked around this in my own project using

@pytest.fixture(autouse=True)
# autouse fixtures with leading underscores are ignored. See https://github.com/pytest-dev/pytest/issues/12404.
def emulate_pytest_randomly_pull_request_617(request: pytest.FixtureRequest) -> None:  # noqa: PT004
    # pytest-randomly sets `randomly_seed` to an integer at pytest_configure time. See
    # https://github.com/pytest-dev/pytest-randomly/blob/8a3a241/src/pytest_randomly/__init__.py#L138.
    #
    # We include that integer to preserve the behavior of an explicit `--randomly-seed <int>` flag
    # passed on the command line while also giving each test a unique seed.
    request.config.option.randomly_seed += int(hashlib.sha512(request.node.nodeid.encode()).hexdigest(), base=16)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

provide unique seed to each test
4 participants