diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 36607e5..5086ae6 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,8 @@ Unreleased * Drop Python 3.10 support. +* Initialize the configured seed when the report header is disabled, including quiet output and runs without the terminal plugin. + 5.0.0 (2026-09-01) ------------------ diff --git a/src/pytest_randomly/__init__.py b/src/pytest_randomly/__init__.py index f9f724a..7b14de0 100644 --- a/src/pytest_randomly/__init__.py +++ b/src/pytest_randomly/__init__.py @@ -14,7 +14,7 @@ from _pytest.config.argparsing import Parser from _pytest.fixtures import SubRequest from _pytest.nodes import Item -from pytest import Collector, fixture, hookimpl +from pytest import Collector, Session, fixture, hookimpl # factory-boy try: @@ -184,9 +184,12 @@ def _reseed(config: Config, offset: int = 0) -> int: return seed +def pytest_sessionstart(session: Session) -> None: + _reseed(session.config) + + def pytest_report_header(config: Config) -> str: seed = config.getoption("randomly_seed") - _reseed(config) return f"Using --randomly-seed={seed}" diff --git a/tests/test_pytest_randomly.py b/tests/test_pytest_randomly.py index b9b142e..0fcecda 100644 --- a/tests/test_pytest_randomly.py +++ b/tests/test_pytest_randomly.py @@ -58,6 +58,32 @@ def test_it_reports_a_header_when_set(simpletester): assert lines == ["Using --randomly-seed=10"] +@pytest.mark.parametrize( + "output_args", [(), ("-q",), ("-qq",), ("--no-header",), ("-p", "no:terminal")] +) +def test_initial_seed_does_not_require_a_report_header(ourtester, output_args): + ourtester.makepyfile( + test_one=""" + import random + + expected = random.Random(123) + + def test_a(): + assert random.random() == expected.random() + + def test_b(): + assert random.random() == expected.random() + """ + ) + out = ourtester.runpytest_subprocess( + "--randomly-seed=123", + "--randomly-dont-reorganize", + "--randomly-dont-reset-seed", + *output_args, + ) + assert out.ret == 0 + + def test_it_uses_different_random_seeds_per_test(ourtester): """ Run a pair of tests that generate the a number and then assert they got