-
Hi there, I'm trying to use polyfactory in combination with syrupy. I'm struggling with the issue that even if I set
I still get different objects in each of my test, and (what's my bigger problem), I get different objects depending on how many of my tests I run. Is it possible to set polyfactory up in a way that each Minimal Example: This runs fine from faker import Faker
import pytest
from polyfactory.factories.pydantic_factory import ModelFactory
from enum import Enum
from pydantic import BaseModel
class DataType(str, Enum):
P_A = "A"
P_B = "B"
P_C = "C"
class Data(BaseModel):
data_type: DataType
value: int
class Report(BaseModel):
inputs: list[Data]
class DataFactory(ModelFactory[Data]):
__allow_none_optionals__ = False
__random_seed__ = 1
__set_as_default_factory_for_type__ = True
class ReportFactory(ModelFactory[Report]):
__allow_none_optionals__ = False
__random_seed__ = 1
@pytest.fixture(scope="function", autouse=True)
def faker_seed():
return 42
def test_creation1(faker: Faker, faker_seed):
ReportFactory.__faker__ = faker
data = ReportFactory.build()
assert data.model_dump(mode="json") == {
"inputs": [{"data_type": "A", "value": 2201}]
}
def test_creation2(faker: Faker, faker_seed):
ReportFactory.__faker__ = faker
data = ReportFactory.build()
assert data.model_dump(mode="json") == {
"inputs": [{"data_type": "C", "value": 9325}]
} if I now add def test_creation0(faker: Faker, faker_seed):
ReportFactory.__faker__ = faker
data = ReportFactory.build()
assert data.model_dump(mode="json") == {
"inputs": [{"data_type": "A", "value": 2201}]
} both my existing tests fail, while this new one works. It essentially takes the result that otherwise |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Heya, does #460 help? |
Beta Was this translation helpful? Give feedback.
-
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:
|
Beta Was this translation helpful? Give feedback.
You could do the following:
A few points to note:
seed_random
method on the factories, but using that won't work as expected because that also reseeds the underlyingFaker
instance being used by the factory.__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 fromModelFactory
. In that case, you'll have to explicitly do the same as above for all those …