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

How to ensure each xdist process gets a different random seed reproducibly? #327

Closed
l0b0 opened this issue Mar 12, 2021 · 1 comment
Closed

Comments

@l0b0
Copy link

l0b0 commented Mar 12, 2021

I'm working on a project which uses random generators to make sure tests which generate things like database entries don't create duplicates. I've combined this with pytest-randomly and "--randomly-seed=${GITHUB_RUN_ID}" --randomly-dont-reset-seed to have reproducible randomness: if the CI pipeline fails I can easily re-run the exact same thing locally. This is working fine, but when combined with pytest-xdist it looks like each worker is getting the same initial seed, and so the tests are back to generating duplicate values.

Is there some way to send a reproducible but different seed to each test when combined with python-xdist?

@adamchainz
Copy link
Member

pytest-randomly goes out of its way to ensure each process gets the same seed, otherwise tests won't be reproducible. The splitting between workers is non-deterministic since it relies on test runtime, which is noisy.

If you need to ensure different database records are created from different processes, you can use os.getpid() for a different number in each process. This could be used in a hash or as a seed to a local Random() instance (which pytest-randomly won't affect)

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

No branches or pull requests

2 participants