Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for randomly-repeat-last #164

Merged
merged 1 commit into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To namespace the cache value (since it's all one big key-value store in pytest), I've changed this to randomly_last_seed - inline with the command-line options all starting with randomly!

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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makepyfile doesn't need re-running between runpytest runs, as the files are left in place

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
yoyoyopcp marked this conversation as resolved.
Show resolved Hide resolved


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