diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index d6084c5..af1b3a6 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -2,6 +2,10 @@
Changelog
=========
+* Reset `Polyfactory `__’s default random state at the start of every test, if it is installed.
+
+ Thanks to davidszotten for the request in `Issue #720 `__.
+
4.1.0 (2026-04-20)
------------------
diff --git a/README.rst b/README.rst
index b9890eb..d497bb4 100644
--- a/README.rst
+++ b/README.rst
@@ -64,6 +64,11 @@ All of these features are on by default but can be disabled with flags.
Only its `legacy random state `__ is affected.
+ * `Polyfactory `__
+
+ Only the default random generator is affected. Factories that have called
+ ``seed_random()`` with their own seed are unaffected.
+
* If additional random generators are used, they can be registered under the
``pytest_randomly.random_seeder``
`entry point `_ and
diff --git a/pyproject.toml b/pyproject.toml
index b47ddb3..6fb577f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -50,6 +50,7 @@ test = [
"faker",
"model-bakery>=1.13",
"numpy",
+ "polyfactory",
"pytest>=9",
"pytest-xdist",
]
diff --git a/src/pytest_randomly/__init__.py b/src/pytest_randomly/__init__.py
index 1888f87..e8958f4 100644
--- a/src/pytest_randomly/__init__.py
+++ b/src/pytest_randomly/__init__.py
@@ -54,6 +54,14 @@
except ImportError: # pragma: no cover
have_numpy = False
+# polyfactory
+try:
+ from polyfactory.constants import DEFAULT_RANDOM as polyfactory_random
+
+ have_polyfactory = True
+except ImportError: # pragma: no cover
+ have_polyfactory = False
+
def make_seed() -> int:
return random.Random().getrandbits(32)
@@ -157,6 +165,9 @@ def _reseed(config: Config, offset: int = 0) -> int:
if have_numpy: # pragma: no branch
np_random.seed(seed % 2**32)
+ if have_polyfactory: # pragma: no branch
+ polyfactory_random.setstate(random_state)
+
if entrypoint_reseeds is None:
eps = entry_points(group="pytest_randomly.random_seeder")
entrypoint_reseeds = [e.load() for e in eps]
diff --git a/tests/test_pytest_randomly.py b/tests/test_pytest_randomly.py
index 2846688..73226c6 100644
--- a/tests/test_pytest_randomly.py
+++ b/tests/test_pytest_randomly.py
@@ -696,6 +696,50 @@ def test_one():
out.assert_outcomes(passed=1)
+def test_polyfactory(ourtester):
+ """
+ Check that Polyfactory's default random generator is reset between tests.
+
+ Uses a Union-typed field so the assertion depends on
+ DEFAULT_RANDOM.choice() picking which type to generate. A plain ``int``
+ field wouldn't do: Polyfactory generates those via its Faker instance,
+ whose random generator pytest-randomly already reseeds separately, so
+ such a test would pass even without resetting DEFAULT_RANDOM.
+ """
+ ourtester.makepyfile(
+ test_one="""
+ from dataclasses import dataclass
+ from typing import Union
+
+ from polyfactory.factories import DataclassFactory
+
+
+ @dataclass
+ class Foo:
+ x: Union[int, str, float, bytes]
+
+
+ class FooFactory(DataclassFactory[Foo]):
+ __model__ = Foo
+
+
+ def test_a():
+ value = FooFactory.build().x
+ assert isinstance(value, str)
+ assert value == 'jKdWckZoDYuPmqHSsjBh'
+
+
+ def test_b():
+ value = FooFactory.build().x
+ assert isinstance(value, int)
+ assert value == 1908
+ """
+ )
+
+ out = ourtester.runpytest("--randomly-seed=1")
+ out.assert_outcomes(passed=2)
+
+
def test_failing_import(testdir):
"""Test with pytest raising CollectError or ImportError.