-
Notifications
You must be signed in to change notification settings - Fork 25
/
runner.py
130 lines (117 loc) · 4.26 KB
/
runner.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import re
import sys
def run():
log("Preparing test suite")
testListFile = "test_list.txt"
os.system('pytest --collect-only > {}'.format(testListFile))
log("Reading collected modules file")
collectedData = open(testListFile).read()
os.remove(testListFile)
log("Collecting modules")
testList = re.findall("<Module '(.+)'>", collectedData)
log("Found {} test modules".format(len(testList)))
if not testList:
m = re.search("errors during collection", collectedData)
if m:
log(collectedData)
return -1
retVal = 0
totalPassed = 0
totalFailed = 0
totalSkipped = 0
totalErros = 0
allFailedTests = []
allErrorTests = []
failureData = []
testRep = 'currentTestReport.txt'
passPat = re.compile("==.+ ([0-9]+) passed,?.+===\n")
skipPat = re.compile("==.+ ([0-9]+) skipped,?.+===\n")
failPat = re.compile("==.+ ([0-9]+) failed,?.+===\n")
errPat = re.compile("==.+ ([0-9]+) error,?.+===\n")
failedTestPat = re.compile('____ (test.+) ____')
errorTestPat = re.compile('____ (ERROR.+) ____')
for test in testList:
# testRep = '{}.rep'.format(test.split("/")[-1])
log("Going to run {}".format(test))
r = os.system('pytest -k "{}" > {}'.format(test, testRep))
reportLines = open(testRep).readlines()
output = ''.join(reportLines)
pas = passPat.search(output)
passed = int(pas.groups()[0]) if pas else 0
skp = skipPat.search(output)
skipped = int(skp.groups()[0]) if skp else 0
if r:
fai = failPat.search(output)
err = errPat.search(output)
if not (fai or err):
log("Non zero return value from {} run but no failures "
"or errors reported".format(test))
log(output)
return -1
failed = int(fai.groups()[0]) if fai else 0
errors = int(err.groups()[0]) if err else 0
failedNames = []
errorNames = []
startedF = None
startedE = None
for line in reportLines:
if '= FAILURES =' in line:
startedF = True
startedE = None
continue
if '= ERRORS =' in line:
startedF = None
startedE = True
continue
if startedF:
failureData.append(line)
m = failedTestPat.search(line)
if m:
failedNames.append(m.groups()[0])
if startedE:
failureData.append(line)
m = errorTestPat.search(line)
if m:
errorNames.append(m.groups()[0])
else:
failed = 0
errors = 0
log('In {}, {} passed, {} failed, {} errors, {} skipped'.
format(test, passed, errors, failed, skipped))
if failed:
log("Failed tests: {}".format(', '.join(failedNames)))
for nm in failedNames:
allFailedTests.append((test, nm))
if errors:
log("Error in tests: {}".format(', '.join(errorNames)))
for nm in errorNames:
allErrorTests.append((test, nm))
retVal += r
totalPassed += passed
totalFailed += failed
totalErros += errors
totalSkipped += skipped
log('Total {} passed, {} failed, {} errors, {} skipped'.
format(totalPassed, totalFailed, totalErros, totalSkipped))
if totalFailed:
log("Failed tests:")
for fm, fn in allFailedTests:
log('{}:{}'.format(fm, fn))
if totalErros:
log("Error in tests:")
for fm, fn in allErrorTests:
log('{}:{}'.format(fm, fn))
if failureData:
log("Writing failure data in Test-Report.txt")
with open('../Test-Report.txt', 'w') as f:
f.write(''.join(failureData))
if os.path.exists(testRep):
os.remove(testRep)
log("Tests run. Returning {}".format(retVal))
return retVal
def log(msg):
return print(msg, flush=True)
if __name__ == "__main__":
r = run()
sys.exit(os.EX_SOFTWARE if r > 0 else os.EX_OK)