Skip to content

Commit 7f87b82

Browse files
orangemochapiscisaureus
authored andcommitted
test: runner support for flaky tests
Adding --flaky-tests option, to allow regarding flaky tests failures as non-fatal. Currently only observed by the TapProgressIndicator, which will add a # TODO directive to tests classified as flaky. According to the TAP specification, the test harness is supposed to treat failures that have a # TODO directive as non-fatal.
1 parent 13ad06e commit 7f87b82

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

tools/test.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@
5555

5656
class ProgressIndicator(object):
5757

58-
def __init__(self, cases):
58+
def __init__(self, cases, flaky_tests_mode):
5959
self.cases = cases
60+
self.flaky_tests_mode = flaky_tests_mode
6061
self.queue = Queue(len(cases))
6162
for case in cases:
6263
self.queue.put_nowait(case)
@@ -234,13 +235,19 @@ def HasRun(self, output):
234235
self._done += 1
235236
command = basename(output.command[-1])
236237
if output.UnexpectedOutput():
237-
print 'not ok %i - %s' % (self._done, command)
238+
status_line = 'not ok %i - %s' % (self._done, command)
239+
if FLAKY in output.test.outcomes and self.flaky_tests_mode == "dontcare":
240+
status_line = status_line + " # TODO : Fix flaky test"
241+
print status_line
238242
for l in output.output.stderr.splitlines():
239243
print '#' + l
240244
for l in output.output.stdout.splitlines():
241245
print '#' + l
242246
else:
243-
print 'ok %i - %s' % (self._done, command)
247+
status_line = 'ok %i - %s' % (self._done, command)
248+
if FLAKY in output.test.outcomes:
249+
status_line = status_line + " # TODO : Fix flaky test"
250+
print status_line
244251

245252
duration = output.test.duration
246253

@@ -258,8 +265,8 @@ def Done(self):
258265

259266
class CompactProgressIndicator(ProgressIndicator):
260267

261-
def __init__(self, cases, templates):
262-
super(CompactProgressIndicator, self).__init__(cases)
268+
def __init__(self, cases, flaky_tests_mode, templates):
269+
super(CompactProgressIndicator, self).__init__(cases, flaky_tests_mode)
263270
self.templates = templates
264271
self.last_status_length = 0
265272
self.start_time = time.time()
@@ -314,29 +321,29 @@ def PrintProgress(self, name):
314321

315322
class ColorProgressIndicator(CompactProgressIndicator):
316323

317-
def __init__(self, cases):
324+
def __init__(self, cases, flaky_tests_mode):
318325
templates = {
319326
'status_line': "[%(mins)02i:%(secs)02i|\033[34m%%%(remaining) 4d\033[0m|\033[32m+%(passed) 4d\033[0m|\033[31m-%(failed) 4d\033[0m]: %(test)s",
320327
'stdout': "\033[1m%s\033[0m",
321328
'stderr': "\033[31m%s\033[0m",
322329
}
323-
super(ColorProgressIndicator, self).__init__(cases, templates)
330+
super(ColorProgressIndicator, self).__init__(cases, flaky_tests_mode, templates)
324331

325332
def ClearLine(self, last_line_length):
326333
print "\033[1K\r",
327334

328335

329336
class MonochromeProgressIndicator(CompactProgressIndicator):
330337

331-
def __init__(self, cases):
338+
def __init__(self, cases, flaky_tests_mode):
332339
templates = {
333340
'status_line': "[%(mins)02i:%(secs)02i|%%%(remaining) 4d|+%(passed) 4d|-%(failed) 4d]: %(test)s",
334341
'stdout': '%s',
335342
'stderr': '%s',
336343
'clear': lambda last_line_length: ("\r" + (" " * last_line_length) + "\r"),
337344
'max_length': 78
338345
}
339-
super(MonochromeProgressIndicator, self).__init__(cases, templates)
346+
super(MonochromeProgressIndicator, self).__init__(cases, flaky_tests_mode, templates)
340347

341348
def ClearLine(self, last_line_length):
342349
print ("\r" + (" " * last_line_length) + "\r"),
@@ -737,8 +744,8 @@ def GetVmFlags(self, testcase, mode):
737744
def GetTimeout(self, mode):
738745
return self.timeout * TIMEOUT_SCALEFACTOR[mode]
739746

740-
def RunTestCases(cases_to_run, progress, tasks):
741-
progress = PROGRESS_INDICATORS[progress](cases_to_run)
747+
def RunTestCases(cases_to_run, progress, tasks, flaky_tests_mode):
748+
progress = PROGRESS_INDICATORS[progress](cases_to_run, flaky_tests_mode)
742749
return progress.Run(tasks)
743750

744751

@@ -762,6 +769,7 @@ def BuildRequirements(context, requirements, mode, scons_flags):
762769
TIMEOUT = 'timeout'
763770
CRASH = 'crash'
764771
SLOW = 'slow'
772+
FLAKY = 'flaky'
765773

766774

767775
class Expression(object):
@@ -1209,6 +1217,9 @@ def BuildOptions():
12091217
default=False, action="store_true")
12101218
result.add_option("--cat", help="Print the source of the tests",
12111219
default=False, action="store_true")
1220+
result.add_option("--flaky-tests",
1221+
help="Regard tests marked as flaky (run|skip|dontcare)",
1222+
default="run")
12121223
result.add_option("--warn-unused", help="Report unused rules",
12131224
default=False, action="store_true")
12141225
result.add_option("-j", help="The number of parallel tasks to run",
@@ -1234,6 +1245,13 @@ def ProcessOptions(options):
12341245
VERBOSE = options.verbose
12351246
options.arch = options.arch.split(',')
12361247
options.mode = options.mode.split(',')
1248+
def CheckTestMode(name, option):
1249+
if not option in ["run", "skip", "dontcare"]:
1250+
print "Unknown %s mode %s" % (name, option)
1251+
return False
1252+
return True
1253+
if not CheckTestMode("--flaky-tests", options.flaky_tests):
1254+
return False
12371255
return True
12381256

12391257

@@ -1436,15 +1454,15 @@ def wrap(processor):
14361454

14371455
result = None
14381456
def DoSkip(case):
1439-
return SKIP in case.outcomes or SLOW in case.outcomes
1457+
return SKIP in case.outcomes or SLOW in case.outcomes or (FLAKY in case.outcomes and options.flaky_tests == "skip")
14401458
cases_to_run = [ c for c in all_cases if not DoSkip(c) ]
14411459
if len(cases_to_run) == 0:
14421460
print "No tests to run."
1443-
return 0
1461+
return 1
14441462
else:
14451463
try:
14461464
start = time.time()
1447-
if RunTestCases(cases_to_run, options.progress, options.j):
1465+
if RunTestCases(cases_to_run, options.progress, options.j, options.flaky_tests):
14481466
result = 0
14491467
else:
14501468
result = 1

0 commit comments

Comments
 (0)