Skip to content

Commit

Permalink
Support for randomly-repeat-last
Browse files Browse the repository at this point in the history
This option enables the test to run with the previously used randomly
seed.
  • Loading branch information
Paul Carlisle committed Feb 3, 2019
1 parent 24a2d1c commit 0eb19d0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pytest_randomly.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@

def pytest_addoption(parser):
group = parser.getgroup('randomly', 'Randomizes tests')
group._addoption(
'--randomly-repeat-last', action='store_true', dest='randomly_repeat_last',
help="""Set the seed to the previous run's seed."""
)
group._addoption(
'--randomly-seed', action='store', dest='randomly_seed',
default=int(time.time()), type=int,
Expand Down Expand Up @@ -82,6 +86,15 @@ def _reseed(config, offset=0):
np_random.set_state(np_random_states[seed])


def pytest_configure(config):
if config.option.randomly_repeat_last and config.cache.get('last_seed', None):
seed = config.cache.get('last_seed', None)
else:
seed = config.getoption('randomly_seed')
config.cache.set('last_seed', seed)
config.option.randomly_seed = seed


def pytest_report_header(config):
_reseed(config)
seed = config.getoption('randomly_seed')
Expand Down
34 changes: 34 additions & 0 deletions tests/test_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,40 @@ def test_b():
out.assert_outcomes(passed=2, failed=0)


def test_it_reuses_the_prev_run_seed(ourtestdir):
"""
Run a test that exercises the randomly-repeat-last option.
"""
ourtestdir.makepyfile(
test_one="""
def test_a():
pass
"""
)
out = ourtestdir.runpytest('--randomly-seed=33')
out.assert_outcomes(passed=1, failed=0)
out.stdout.fnmatch_lines(['Using --randomly-seed=33'])
ourtestdir.makepyfile(
test_one="""
def test_a():
pass
"""
)
out = ourtestdir.runpytest('--randomly-repeat-last')
out.assert_outcomes(passed=1, failed=0)
out.stdout.fnmatch_lines(['Using --randomly-seed=33'])
ourtestdir.makepyfile(
test_one="""
def test_a():
pass
"""
)
out = ourtestdir.runpytest()
out.assert_outcomes(passed=1, failed=0)
for line in out.outlines:
assert 'Using --randomly-seed=33' not in line


def test_it_resets_the_random_seed_at_the_start_of_test_classes(ourtestdir):
ourtestdir.makepyfile(
test_one="""
Expand Down

0 comments on commit 0eb19d0

Please sign in to comment.