-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCucumberTestRerunner.java
More file actions
99 lines (86 loc) · 3.35 KB
/
CucumberTestRerunner.java
File metadata and controls
99 lines (86 loc) · 3.35 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
99
package testrunner;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.engine.TestExecutionResult.Status;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.engine.discovery.UriSelector;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;
/**
* Custom test runner for rerunning failed Cucumber tests.
*/
public class CucumberTestRerunner {
private static final Logger logger = LoggerFactory.getLogger(CucumberTestRerunner.class);
/**
* Main method to execute the rerun of failed Cucumber tests.
*
* @param args Command line arguments (not used).
* @throws IOException If an I/O error occurs.
*/
public static void main(String[] args) throws IOException {
var retryFile = Path.of("target/failedScenarios.txt");
if (!Files.exists(retryFile)) {
logger.info(() -> "No retry file has been found");
return;
}
cleanUpDirectories("target/logs", "target/trace");
List<UriSelector> selectors = Files.readAllLines(retryFile).stream()
.map(DiscoverySelectors::selectUri)
.toList();
if (selectors.isEmpty()) {
logger.info(() -> "No failed tests found which require retest");
return;
}
logger.info(() -> String.format("Found %d tests requiring a rerun", selectors.size()));
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectors)
.build();
Launcher launcher = LauncherFactory.create();
SummaryGeneratingListener listener = new SummaryGeneratingListener();
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);
TestExecutionSummary summary = listener.getSummary();
System.exit(
summary.getTotalFailureCount() > 0 ? Status.FAILED.ordinal() : Status.SUCCESSFUL.ordinal());
}
/**
* Cleans up the specified directories by deleting their contents.
*
* @param directories The directories to clean up.
* @throws IOException If an I/O error occurs during directory deletion.
*/
private static void cleanUpDirectories(String... directories) throws IOException {
for (String dir : directories) {
deleteDirectory(Path.of(dir));
}
}
/**
* Deletes the contents of the specified directory.
*
* @param path The path to the directory to delete.
* @throws IOException If an I/O error occurs during directory deletion.
*/
private static void deleteDirectory(Path path) throws IOException {
if (Files.exists(path)) {
try (Stream<Path> pathStream = Files.walk(path)) {
pathStream.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
} catch (IOException e) {
logger.error(e, () -> String.format("Failed to delete directory: %s", path));
throw e;
}
}
}
}