Reputation: 116
From the documentation, I know that one can enforce a specific seed for a given ModelFactory
, as follows:
class PersonFactory(DataclassFactory[Person]):
__random_seed__ = 1
Now, I could not find any information about "global" configuration of the factories. In the case of a project containing hundreds of factory models, can one just enforce the seed once for all to control the randomness in general, and not manually write the same parameter hundreds of times?
Upvotes: 0
Views: 377
Reputation: 116
The solution I finally came across is about resolving the seed via pytest fixtures:
from faker import Faker
from polyfactory.factories.pydantic_factory import ModelFactory
@pytest.fixture(autouse=True)
def faker_seed():
return 1
@pytest.fixture(autouse=True)
def seed_factories(faker: Faker, faker_seed) -> None:
ModelFactory.__faker__ = faker
ModelFactory.__random__.seed(faker_seed)
When placed in the main conftest.py
file, this code makes all factories produce deterministic instances.
Upvotes: 2