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

pytest-replay does not respect the order of the replay file #53

Merged
merged 6 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
repos:
- repo: https://github.com/psf/black
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.12.1
hooks:
- id: black
Expand Down
17 changes: 9 additions & 8 deletions src/pytest_replay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,23 @@ def pytest_collection_modifyitems(self, items, config):

with open(replay_file, "r", encoding="UTF-8") as f:
all_lines = f.readlines()
nodeids = {
nodeids = dict.fromkeys(
DavideCanton marked this conversation as resolved.
Show resolved Hide resolved
nicoddemus marked this conversation as resolved.
Show resolved Hide resolved
json.loads(line)["nodeid"]
for line in all_lines
if not line.strip().startswith(("#", "//"))
}
)

items_dict = {item.nodeid: item for item in items}
remaining = []
deselected = []
for item in items:
if item.nodeid in nodeids:
for nodeid in nodeids:
DavideCanton marked this conversation as resolved.
Show resolved Hide resolved
if item := items_dict.pop(nodeid):
remaining.append(item)
else:
deselected.append(item)
deselected = list(items_dict.values())

if deselected:
config.hook.pytest_deselected(items=deselected)
items[:] = remaining

items[:] = remaining

def append_test_to_script(self, nodeid, line):
suffix = "-" + self.xdist_worker_name if self.xdist_worker_name else ""
Expand Down
52 changes: 52 additions & 0 deletions tests/test_replay.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import itertools as it
import json

import pytest
Expand Down Expand Up @@ -216,3 +217,54 @@ def test_2():
expected = {"test_cwd_changed.py::test_1", "test_cwd_changed.py::test_2"}
assert contents == expected
assert result.ret == 0


@pytest.mark.usefixtures("suite")
def test_execution_different_order(testdir):
DavideCanton marked this conversation as resolved.
Show resolved Hide resolved
dir = testdir.tmpdir / "replay"
options = [f"--replay-record-dir={dir}"]
result = testdir.runpytest(*options)

replay_file = dir / ".pytest-replay.txt"

with replay_file.open("r+") as f:
content = f.readlines()

# pairwise shuffle of replay file
pairs = [(content[i], content[i + 1]) for i in range(0, len(content), 2)]
pairs = [pairs[2], pairs[0], pairs[3], pairs[1]]
content = list(it.chain.from_iterable(pairs))

f.seek(0)
f.writelines(content)

result = testdir.runpytest(f"--replay={replay_file}", "-v")
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"test_2.py::test_zz*25%*",
"test_1.py::test_foo*50%*",
"test_3.py::test_foobar*75%*",
"test_1.py::test_bar*100%*",
],
consecutive=True,
)


@pytest.mark.usefixtures("suite")
def test_filter_out_tests_not_in_file(testdir):
DavideCanton marked this conversation as resolved.
Show resolved Hide resolved
dir = testdir.tmpdir / "replay"
options = [f"--replay-record-dir={dir}", "-k", "foo"]
result = testdir.runpytest(*options)

replay_file = dir / ".pytest-replay.txt"

result = testdir.runpytest(f"--replay={replay_file}", "-v")
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"test_1.py::test_foo*50%*",
"test_3.py::test_foobar*100%*",
],
consecutive=True,
)