-
Notifications
You must be signed in to change notification settings - Fork 2
/
pytest
executable file
·58 lines (41 loc) · 1.54 KB
/
pytest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
""" Wraps py.test to rerun on failures up to n times.
This is very similar to the pytest-rerunfailures plugin, but has the following
features that the plugin does not provide:
- Always show the full test output in our output
- The failed tests are retried at the end of each test run.
Example:
./pytest . --reruns=1
Note, this is only meant to be used for our scheduled test suite. You generally
want to use py.test directly.
"""
import argparse
import os
import shlex
import subprocess
import sys
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--reruns', type=int, default=0)
def main(reruns: int, args: list[str]) -> int:
result = None
if reruns < 0 or 10 < reruns:
print(f"Invalid reruns: {reruns}, expected 0-10")
return 1
for run in range(1, reruns + 2):
if run == 1:
# Clear the cache of old rerun failures the first time
cmd = ('pytest', *args, '--cache-clear')
else:
# Only run failures in consecutive calls
cmd = ('pytest', *args, '--last-failed')
print(f"Running run #{run}: {shlex.join(cmd)}", flush=True)
result = subprocess.run(cmd, check=False, env={
'TEST_RUN': str(run), **os.environ,
})
# Abort as soon as a run was successful
if result.returncode == 0:
break
return result.returncode if result is not None else 1
if __name__ == '__main__':
known, args = parser.parse_known_args()
sys.exit(main(**vars(known), args=args))