A test that sometimes fails and sometimes passes on the same code is flaky. Sort the tests of a CI history into groups.
What to build
A program that classifies tests by their results over several runs.
Requirements
- Each line is
NAME RESULTS, where RESULTS is a string ofPandF, one letter per run, oldest first. All tests have the same number of runs R. - Check in this order. All
P: printNAME: ok. AllF:NAME: broken. Only the last letter isFand the restP:NAME: regression. Anything else:NAME: flaky (F of R failed)where F is the number ofFletters. - Finally print
ok: A, flaky: B, regression: C, broken: Dwith the number of tests in each group.
Input
The first line has n (1 to 50). Each of the next n lines is NAME RESULTS.
Output
One line per test, then the summary line.
Constraints
1 ≤ n ≤ 50, 1 ≤ R ≤ 20.
Example
Input:
4
test_add PPPPP
test_net PFPPF
test_parse PPPPF
test_old FFFFF
Output:
test_add: ok
test_net: flaky (2 of 5 failed)
test_parse: regression
test_old: broken
ok: 1, flaky: 1, regression: 1, broken: 1
Why it works
The order of the checks matters: a test with only the last run failed looks flaky at first sight, but it is far more likely a real new break, so it is named before the general mixed case.
Examples
4 test_add PPPPP test_net PFPPF test_parse PPPPF test_old FFFFF
test_add: ok test_net: flaky (2 of 5 failed) test_parse: regression test_old: broken ok: 1, flaky: 1, regression: 1, broken: 1
2 a PPP b FPP
a: ok b: flaky (1 of 3 failed) ok: 1, flaky: 1, regression: 0, broken: 0
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Each line has a name and a string like PPFP with one letter per run.
Hint 2
Check in this order: all P, all F, only the last run F, otherwise flaky.
Hint 3
Count the failures with results.count("F").
Solution
Show one correct solution (try it yourself first)
n = int(input())
groups = {"ok": 0, "flaky": 0, "regression": 0, "broken": 0}
for _ in range(n):
name, results = input().split()
runs = len(results)
failures = results.count("F")
if failures == 0:
kind, text = "ok", "ok"
elif failures == runs:
kind, text = "broken", "broken"
elif failures == 1 and results[-1] == "F":
kind, text = "regression", "regression"
else:
kind, text = "flaky", f"flaky ({failures} of {runs} failed)"
groups[kind] += 1
print(f"{name}: {text}")
print(f"ok: {groups['ok']}, flaky: {groups['flaky']}, regression: {groups['regression']}, broken: {groups['broken']}")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step