Skip to content

Cannot get deterministic randomness to work #578

Answered by guacs
JobaDiniz asked this question in Q&A
Discussion options

You must be logged in to vote

@JobaDiniz what @Alc-Alc said will work. The reason you were getting non-deterministic values is because 1) faker has it's own seeding and 2) you only set the __random__ on the factories you defined which means ModelFactory.__random__ is the default one set by polyfactory without any specific seed value.

Point 2 is also the reason __random_seed__ didn't work for the Use fields. The easiest way to ensure you don't face any of these issues is to:

from polyfactory.factories.pydantic_factory import ModelFactory

ModelFactory.seed_random(10) # You can run this in an `autouse` fixture in `pytest` as well to get determinstic values across tests

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@JobaDiniz
Comment options

@Alc-Alc
Comment options

@guacs
Comment options

@guacs
Comment options

Answer selected by JobaDiniz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants