-
Notifications
You must be signed in to change notification settings - Fork 0
/
RerunMonitor.java
68 lines (62 loc) · 2.42 KB
/
RerunMonitor.java
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
package plugins;
import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.event.EventPublisher;
import io.cucumber.plugin.event.TestCase;
import io.cucumber.plugin.event.TestCaseFinished;
import io.cucumber.plugin.event.TestRunFinished;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
/**
* A Cucumber plugin that monitors test execution and records failed test cases. The failed test
* cases are written to a file for later re-execution.
*/
public class RerunMonitor implements ConcurrentEventListener {
private final List<TestCase> failedTestCaseList = new ArrayList<>();
/**
* Registers event handlers for test case finished and test run finished events.
*
* @param eventPublisher the event publisher
*/
@Override
public void setEventPublisher(EventPublisher eventPublisher) {
eventPublisher.registerHandlerFor(TestCaseFinished.class, this::testCaseFinishedHandler);
eventPublisher.registerHandlerFor(TestRunFinished.class, this::testRunFinishedHandler);
}
/**
* Handles the TestCaseFinished event. Adds the test case to the failed test case list if the test
* did not pass.
*
* @param testCaseFinished the event indicating a test case has finished
*/
private void testCaseFinishedHandler(TestCaseFinished testCaseFinished) {
if (!testCaseFinished.getResult().getStatus().isOk()) {
failedTestCaseList.add(testCaseFinished.getTestCase());
}
}
/**
* Handles the TestRunFinished event. Writes the list of failed test cases to a file.
*
* @param testRunFinished the event indicating the test run has finished
*/
private void testRunFinishedHandler(TestRunFinished testRunFinished) {
Path filePath = Paths.get("target/failedScenarios.txt");
try {
Files.deleteIfExists(filePath);
try (BufferedWriter writer = Files.newBufferedWriter(filePath, StandardOpenOption.CREATE)) {
for (TestCase testCase : failedTestCaseList) {
String formattedFeature = String.format("%s?line=%d%n",
testCase.getUri().toString().replace(":", ":/"), testCase.getLocation().getLine());
writer.write(formattedFeature);
}
}
} catch (IOException e) {
System.out.println("Error writing to the file: " + e.getMessage());
}
}
}