Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestRunner.java
More file actions
Latest commit
90 lines (75 loc) · 3.69 KB
/
Copy pathTestRunner.java
File metadata and controls
90 lines (75 loc) · 3.69 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
importorg.junit.platform.engine.discovery.DiscoverySelectors;
importorg.junit.platform.launcher.*;
importorg.junit.platform.launcher.core.*;
importorg.junit.platform.launcher.listeners.*;
importjava.io.ByteArrayOutputStream;
importjava.io.PrintStream;
importjava.util.*;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassTestRunner {
privatestaticStringescapeForJson(Stringvalue) {
if (value == null) return"";
returnvalue.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r");
}
publicstaticvoidmain(String[] args) {
PrintStreamoriginalOut = System.out;
PrintStreamoriginalErr = System.err;
ByteArrayOutputStreamtestOut = newByteArrayOutputStream();
ByteArrayOutputStreamtestErr = newByteArrayOutputStream();
System.setOut(newPrintStream(testOut));
System.setErr(newPrintStream(testErr));
LauncherDiscoveryRequestrequest = LauncherDiscoveryRequestBuilder.request()
.selectors(DiscoverySelectors.selectClass(MainTest.class))
.build();
SummaryGeneratingListenerlistener = newSummaryGeneratingListener();
Launcherlauncher = LauncherFactory.create();
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);
TestExecutionSummarysummary = listener.getSummary();
// Restore output
System.setOut(originalOut);
System.setErr(originalErr);
StringBuilderjson = newStringBuilder();
json.append("{");
json.append("\"state\": \"").append(summary.getTotalFailureCount() == 0 ? "passed" : "failed").append("\",");
json.append("\"tests_run\": ").append(summary.getTestsStartedCount()).append(",");
json.append("\"passed\": ").append(summary.getTestsSucceededCount()).append(",");
json.append("\"failed\": ").append(summary.getTotalFailureCount()).append(",");
json.append("\"failure_details\": [");
booleanfirst = true;
for (TestExecutionSummary.Failurefailure : summary.getFailures()) {
if (!first) json.append(",");
Stringrawout = testOut.toString() + testErr.toString();
Stringmessage = failure.getException().getMessage() != null ? failure.getException().getMessage() : "";
Stringexpected = "";
Stringreceived = "";
Patternpattern1 = Pattern.compile("expected:\\s*<(.*?)>\\s*but was:\\s*<(.*?)>");
Matchermatcher1 = pattern1.matcher(message);
if (matcher1.find()) {
expected = matcher1.group(1);
received = matcher1.group(2);
} else {
Patternpattern2 = Pattern.compile("(.*?)\\s*[!=]=\\s*(.*)");
Matchermatcher2 = pattern2.matcher(message);
if (matcher2.find()) {
received = matcher2.group(1).trim();
expected = matcher2.group(2).trim();
}
}
json.append("{");
json.append("\"test_case\": \"").append(failure.getTestIdentifier().getDisplayName()).append("\",");
json.append("\"expected\": \"").append(escapeForJson(expected)).append("\",");
json.append("\"received\": \"").append(escapeForJson(received)).append("\",");
json.append("\"error_message\": \"").append(escapeForJson(message)).append("\",");
json.append("\"rawout\": \"").append(escapeForJson(rawout)).append("\"");
json.append("}");
first = false;
}
json.append("]}");
System.out.println(json.toString());
}
}