Skip to content

Reset Polyfactory's random state on each test#735

Open
pctablet505 wants to merge 2 commits into
pytest-dev:mainfrom
pctablet505:reseed-polyfactory
Open

Reset Polyfactory's random state on each test#735
pctablet505 wants to merge 2 commits into
pytest-dev:mainfrom
pctablet505:reseed-polyfactory

Conversation

@pctablet505

Copy link
Copy Markdown

Polyfactory keeps its own default random.Random() instance (polyfactory.constants.DEFAULT_RANDOM) separate from Faker's, so it wasn't being reseeded along with the other libraries pytest-randomly already supports.

This adds Polyfactory to the same try/except reseed pattern used for factory_boy, Faker, Model Bakery, and NumPy, so factories using the default random generator get deterministic, per-test values under --randomly-seed. Factories that call seed_random() themselves are unaffected, same as before.

Closes #720.

Polyfactory maintains its own default random.Random() instance
(polyfactory.constants.DEFAULT_RANDOM) separate from Faker's, so it
wasn't being reseeded alongside the other supported libraries.

Follows the existing pattern used for factory_boy, Faker, Model
Bakery, and NumPy: reseed if the library is importable.

Closes pytest-dev#720.
The int field was generated via Polyfactory's Faker instance, whose
random generator pytest-randomly already reseeds independently of this
change, so the test passed whether or not DEFAULT_RANDOM was reset.

Switch to a Union-typed field so the assertion depends on
DEFAULT_RANDOM.choice() picking which type to build, which does
regress to a different value if the reseed is removed.
@pctablet505
pctablet505 marked this pull request as ready for review July 17, 2026 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

could we also reseed polyfactory?

1 participant