-
Notifications
You must be signed in to change notification settings - Fork 31
Random seed appears to also seed other random() calls #531
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
Comments
It's not "leaking over", setting the global random seed is one of pytest-randomly's features. README:
You can disable this feature or use a local |
@adamchainz Thank you for the quick response. Understood that this is not a bug. I assumed that pytest-randomly would not affect other randomness but in hindsight I guess it's kind of obvious. Thanks for the suggestion on the local |
I just got hit by this one as well. I didn't put two and two together, when I read the documentation, to realize that Randomly fixates I get the solution of using random.Random(), but as we are many developers it's not a feasible solution to rely on every developer to remember not to use random in our code base. Would it be possible for Randomly to base the seed on an instance of random: |
There's also
I don't understand what you're suggesting. The seed is already from an instance of
|
@adamchainz I think the problem is that developer's probably don't expect the random seed generated by pytest to affect all other random calls in their test suite. @MichaelSoegaard, This might be unavoidable, since the whole point of pytest-randomly is to set a global random seed that can be reproduced. If pytest-randomly doesn't affect the global scope, then we lose that reproducibility, which is more important IMO. |
Yeah, I get it now. -and you're right @medley56, it hadn't crossed my mind that it would actually also affect our calls to If I could choose, I would like to just have the tests randomized while I don't care about getting the global seed locked. It would be possible if all the tests were just randomized without locking the seed and then index the order of the tests for reproducibility. I apparently just misunderstood the core purpose of the package. |
Use
The tagline is "Pytest plugin to randomly order tests and control random.seed." Happy to take suggestions to improve that. |
Thank you @adamchainz
-and my comment wasn't meant as being sarcastic. I meant it when I said I just misunderstood it. :-) Keep up the great work. |
Just encountered this unexpectedly and found it surprising. I wonder if we could change the behavior so that each test gets a unique (but deterministic) seed. The test node id could be used as part of the input to create the seed. This would preserve the reproducibility of test runs while also preventing scenarios like "my randomly generated test bucket was the same as the bucket used in another test" |
Please can you write a new issue rather than commenting on this old one? |
Python Version
3.9.0
pytest Version
7.2.2
Package Version
3.12.0
Description
In our tests, we regularly create mocked AWS S3 buckets with random 16 character names. e.g.
bucket_name = ''.join(random.choice(string.ascii_letters) for i in range(16))
Those buckets are created via a fixture that yields a function called
create_mock_bucket()
and the scope of that fixture is set to the default (per function).When pytest-randomly is enabled, those bucket names all end up being exactly the same string for a given pytest execution. When run with
-p no:randomly
, the bucket names appear random as expected and all the tests pass. I suspect that the random seed for pytest-randomly is somehow leaking over into seeding our call to random.choice but that is purely speculation.NB: This has exposed another weakness in our testing, namely that our mocked buckets are persisting between tests. I'm running that down separately but the behavior described above seems unintentional.
The text was updated successfully, but these errors were encountered: