File tree Expand file tree Collapse file tree 4 files changed +52
-1
lines changed Expand file tree Collapse file tree 4 files changed +52
-1
lines changed Original file line number Diff line number Diff line change 22Changelog
33=========
44
5+ 3.16.0 (2024-04-09)
6+ -------------------
7+
8+ * Add the option ``--randomly-seed-per-test `` to use a different seed for each test.
9+
10+ Resolves `Issue #600 <https://github.com/pytest-dev/pytest-randomly/issues/600 >`__
11+
5123.15.0 (2023-08-15)
613-------------------
714
Original file line number Diff line number Diff line change @@ -160,6 +160,9 @@ You can disable behaviours you don't like with the following flags:
160160 the start of every test
161161* ``--randomly-dont-reorganize `` - turn off the shuffling of the order of tests
162162
163+ By default each test starts out with the same seed, if you'd like a different one
164+ per test you can use the ``--randomly-seed-per-test `` flag.
165+
163166The plugin appears to Pytest with the name 'randomly'. To disable it
164167altogether, you can use the ``-p `` argument, for example:
165168
Original file line number Diff line number Diff line change @@ -95,6 +95,15 @@ def pytest_addoption(parser: Parser) -> None:
9595 Default behaviour: use random.Random().getrandbits(32), so the seed is
9696 different on each run.""" ,
9797 )
98+ group ._addoption (
99+ "--randomly-seed-per-test" ,
100+ action = "store_true" ,
101+ dest = "randomly_seed_per_test" ,
102+ default = False ,
103+ help = """Use a different seed for each test. Can be helpful for getting
104+ different random data in each test, but still having reproducible
105+ tests. Default behaviour: False.""" ,
106+ )
98107 group ._addoption (
99108 "--randomly-dont-reset-seed" ,
100109 action = "store_false" ,
@@ -209,9 +218,17 @@ def pytest_runtest_setup(item: Item) -> None:
209218 _reseed (item .config , - 1 )
210219
211220
221+ def seed_from_string (string : str ) -> int :
222+ return int (hashlib .md5 (string .encode ()).hexdigest (), 16 )
223+
224+
212225def pytest_runtest_call (item : Item ) -> None :
213226 if item .config .getoption ("randomly_reset_seed" ):
214- _reseed (item .config )
227+ if item .config .getoption ("randomly_seed_per_test" ):
228+ test_offset = seed_from_string (item .nodeid ) + 100
229+ else :
230+ test_offset = 0
231+ _reseed (item .config , offset = test_offset )
215232
216233
217234def pytest_runtest_teardown (item : Item ) -> None :
Original file line number Diff line number Diff line change @@ -82,6 +82,30 @@ def test_b():
8282 out .assert_outcomes (passed = 2 , failed = 0 )
8383
8484
85+ def test_it_can_use_different_random_seed_per_test (ourtester ):
86+ """
87+ Run a pair of tests that generate a number and assert they produce
88+ different numbers.
89+ """
90+ ourtester .makepyfile (
91+ test_one = """
92+ import random
93+
94+ def test_a():
95+ test_a.num = random.random()
96+ if hasattr(test_b, 'num'):
97+ assert test_a.num != test_b.num
98+
99+ def test_b():
100+ test_b.num = random.random()
101+ if hasattr(test_a, 'num'):
102+ assert test_b.num != test_a.num
103+ """
104+ )
105+ out = ourtester .runpytest ("--randomly-seed-per-test" )
106+ out .assert_outcomes (passed = 2 , failed = 0 )
107+
108+
85109def test_without_cacheprovider (ourtester ):
86110 ourtester .makepyfile (
87111 test_one = """
You can’t perform that action at this time.
0 commit comments