Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions rerun_failed_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def parse_test_results(file_path):
def rerun_failed_tests(failed_tests):
if failed_tests:
print("Initial failed tests:", failed_tests)
failed_test_names = ' '.join(failed_tests)
result = subprocess.run(f'pytest --headless {failed_test_names}', shell=True, capture_output=True, text=True)
result = subprocess.run(['pytest', '--headless', *failed_tests], capture_output=True, text=True)
print(result.stdout)
print(result.stderr)
else:
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_rerun_failed_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import importlib.util
import os
from unittest import mock

_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
_SPEC = importlib.util.spec_from_file_location(
"rerun_failed_tests", os.path.join(_ROOT, "rerun_failed_tests.py")
)
rerun = importlib.util.module_from_spec(_SPEC)
_SPEC.loader.exec_module(rerun)


def test_rerun_failed_tests_does_not_use_a_shell():
names = ["test_a", "test_b; echo injected", "test_c$(id)"]
with mock.patch.object(rerun.subprocess, "run") as run:
run.return_value = mock.Mock(stdout="", stderr="")
rerun.rerun_failed_tests(names)

args, kwargs = run.call_args
assert not kwargs.get("shell")
assert args[0] == ["pytest", "--headless", *names]


def test_parse_test_results_returns_failed_names(tmp_path):
report = tmp_path / "junit.xml"
report.write_text(
"<testsuite>"
'<testcase name="test_ok"/>'
'<testcase name="test_bad"><failure message="x"/></testcase>'
"</testsuite>"
)
assert rerun.parse_test_results(str(report)) == ["test_bad"]