Skip to content

Commit 49c8c1b

Browse files
authored
Fix a crash with Faker installed and plugin disabled (#721)
Fixes #718.
1 parent c9181c2 commit 49c8c1b

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
Changelog
33
=========
44

5+
* Fix a crash with Faker installed when explicitly enabling and disabling the plugin (via ``-p randomly -p no:randomly``).
6+
7+
Thanks to mojosan77 for the report in `Issue #718 <https://github.com/pytest-dev/pytest-randomly/issues/718>`__.
8+
59
* Drop Python 3.9 support.
610

711
4.0.1 (2025-09-12)

src/pytest_randomly/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,15 @@ def _crc32(string: str) -> int:
265265
if have_faker: # pragma: no branch
266266

267267
@fixture(autouse=True)
268-
def faker_seed(pytestconfig: Config, request: SubRequest) -> int:
269-
result: int = pytestconfig.getoption("randomly_seed") + _crc32(
270-
request.node.nodeid
271-
)
272-
return result
268+
def faker_seed(pytestconfig: Config, request: SubRequest) -> Any:
269+
from faker.contrib.pytest.plugin import DEFAULT_SEED
270+
271+
seed = pytestconfig.getoption("randomly_seed")
272+
if seed in ("default", "last"):
273+
# pytest-randomly has been imported but disabled, so
274+
# pytest_configure hasn't run to set the seed. Fall back to
275+
# Faker's default seed.
276+
return DEFAULT_SEED
277+
else:
278+
result: int = seed + _crc32(request.node.nodeid)
279+
return result

tests/test_pytest_randomly.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,18 @@ def test_two(faker):
633633
out.assert_outcomes(passed=2)
634634

635635

636+
def test_faker_enabled_disabled(monkeypatch, ourtester):
637+
ourtester.makepyfile(
638+
test_one="""
639+
def test_one(faker):
640+
assert faker.name() == 'Norma Fisher'
641+
"""
642+
)
643+
644+
out = ourtester.runpytest("-p", "randomly", "-p", "no:randomly")
645+
out.assert_outcomes(passed=1)
646+
647+
636648
def test_model_bakery(ourtester):
637649
"""
638650
Check the Model Bakery random generator is reset between tests.

0 commit comments

Comments
 (0)