Skip to content

How to deterministically create the same objects #500

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

You must be logged in to vote

You could do the following:

@pytest.fixture(autouse=True)
def seed_factories(faker: Faker) -> None:
    ModelFactory.__faker__ = faker
    ModelFactory.__random__.seed(1) # you could use `faker_seed` here as well

A few points to note:

  • There is a seed_random method on the factories, but using that won't work as expected because that also reseeds the underlying Faker instance being used by the factory.
  • If you've overriden the __faker__ or __random__ instance on any of the factories you've created, then this won't have any affect since then those factories will use that instance instead of the one from ModelFactory. In that case, you'll have to explicitly do the same as above for all those …

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@Gitznik
Comment options

Comment options

You must be logged in to vote
1 reply
@Gitznik
Comment options

Answer selected by Gitznik
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