Repository navigation
Expand file tree
/
Copy pathFlakyAnalyzer.java
More file actions
98 lines (82 loc) · 3.46 KB
/
Copy pathFlakyAnalyzer.java
File metadata and controls
98 lines (82 loc) · 3.46 KB
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
package com.abn.design.leetcode;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class FlakyAnalyzer {
static class TestRun {
public int id;
public List<TestResult> results;
}
@JsonIgnoreProperties(ignoreUnknown = true)
static class TestResult {
public String name;
public String status;
}
static class TestStatus {
public String name;
public int passed;
public int failed;
public int totalRuns;
public double failRate;
public String verdict;
}
static List<TestRun> parse(String json) throws Exception {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, new TypeReference<List<TestRun>>() {});
}
public static List<TestStatus> analyze(List<TestRun> runs) {
Map<String, TestStatus> testStatusMap = new LinkedHashMap<>();
TestStatus testStatus;
for(TestRun r : runs) {
for( TestResult res: r.results) {
if(!testStatusMap.containsKey(res.name)) {
testStatus = new TestStatus();
testStatus.name = res.name;
testStatusMap.put(res.name, testStatus);
}
testStatus = testStatusMap.get(res.name);
testStatus.totalRuns ++;
if(res.status.equals("FAIL")) {
testStatus.failed ++;
} else {
testStatus.passed ++;
}
}
}
for(TestStatus ts : testStatusMap.values()) {
if(ts.passed == 0) {
ts.verdict = "BROKEN";
} else if(ts.failed == 0) {
ts.verdict = "STABLE";
} else {
ts.verdict = "FLAKY";
}
ts.failRate = (double) ts.failed / ts.totalRuns;
}
return new ArrayList<>(testStatusMap.values());
}
static void print(List<TestStatus> stats) {
System.out.printf("%-10s %-8s %-8s %-8s %s%n", "Test", "Passes", "Fails", "FailureRate", "Verdict");
for (TestStatus s : stats) {
System.out.printf("%-10s %-8d %-8d %-8s %s%n",
s.name, s.passed, s.failed, s.failRate, s.verdict);
}
}
public static void main(String[] args)
throws Exception {
String json = """
[
{"id":1,"results":[{"name":"login","status":"PASS"},{"name":"logout","status":"FAIL"},{"name":"search","status":"PASS"},{"name":"payment","status":"FAIL"}]},
{"id":2,"results":[{"name":"login","status":"PASS"},{"name":"logout","status":"PASS"},{"name":"search","status":"PASS"},{"name":"payment","status":"FAIL"}]},
{"id":3,"results":[{"name":"login","status":"PASS"},{"name":"logout","status":"FAIL"},{"name":"search","status":"FAIL"},{"name":"payment","status":"FAIL"}]},
{"id":4,"results":[{"name":"login","status":"PASS"},{"name":"logout","status":"PASS"},{"name":"search","status":"PASS"},{"name":"payment","status":"FAIL"}]},
{"id":5,"results":[{"name":"login","status":"PASS"},{"name":"logout","status":"FAIL"},{"name":"search","status":"PASS"},{"name":"payment","status":"FAIL"}]}
]
""";
print(analyze(parse(json)));
}
}