@Override public void executionStarted(final TestIdentifier testIdentifier) { if (testIdentifier.isTest()) { final Optional<MethodSource> methodSource = testIdentifier.getSource() .filter(MethodSource.class::isInstance) .map(MethodSource.class::cast); final String uuid = tests.get(); final TestResult result = new TestResult() .withUuid(uuid) .withName(testIdentifier.getDisplayName()) .withLabels(getTags(testIdentifier)) .withHistoryId(getHistoryId(testIdentifier)) .withStage(Stage.RUNNING); methodSource.ifPresent(source -> { result.setDescription(getDescription(source)); result.getLabels().add(new Label().withName("suite").withValue(getSuite(source))); result.getLabels().add(new Label().withName("package").withValue(source.getClassName())); }); getLifecycle().scheduleTestCase(result); getLifecycle().startTestCase(uuid); } }
public final Builder addResult(TestIdentifier identifier, TestExecutionResult result) { DisplayName displayName = getDisplayName(identifier); if (identifier.isTest()) { testsBuilder().add(displayName); } switch (result.getStatus()) { case SUCCESSFUL: successfulBuilder().add(displayName); return this; case FAILED: failuresBuilder().put(displayName, result.getThrowable().orElse(null)); return this; default: throw new AssertionError("Unhandled case in enum: " + result.getStatus()); } }
private DisplayName getDisplayName(TestIdentifier testIdentifier) { LinkedList<String> names = new LinkedList<>(); Optional<TestIdentifier> id = Optional.of(testIdentifier); do { TestIdentifier identifier = id.get(); Optional<ClassSource> classSource = identifier.getSource() .filter(source -> source instanceof ClassSource) .map(source -> (ClassSource) source) .filter(source -> !source.getPosition().isPresent()) .filter(source -> classesToSkip.contains(source.getJavaClass())); if (classSource.isPresent()) { break; } names.addFirst(identifier.getDisplayName()); id = id.flatMap(testPlan::getParent); } while (id.isPresent()); return DisplayName.create(names); }
@Override public void executionFinished(final TestIdentifier testIdentifier, final TestExecutionResult testExecutionResult) { if (testIdentifier.isTest()) { final String uuid = tests.get(); tests.remove(); getLifecycle().updateTestCase(uuid, result -> { result.setStage(Stage.FINISHED); switch (testExecutionResult.getStatus()) { case FAILED: testExecutionResult.getThrowable().ifPresent(throwable -> { result.setStatus(getStatus(throwable)); result.setStatusDetails(ResultsUtils.getStatusDetails(throwable).orElse(null)); }); break; case SUCCESSFUL: result.setStatus(PASSED); break; default: result.setStatus(SKIPPED); testExecutionResult.getThrowable().ifPresent(throwable -> result.setStatusDetails(ResultsUtils.getStatusDetails(throwable).orElse(null)) ); break; } }); getLifecycle().stopTestCase(uuid); getLifecycle().writeTestCase(uuid); } }
public final Builder addSkipped(TestIdentifier identifier, @Nullable String reason) { DisplayName displayName = getDisplayName(identifier); if (identifier.isTest()) { testsBuilder().add(displayName); } skippedBuilder().put(displayName, reason); return this; }
public static Description toDescription(TestIdentifier testIdentifier) { String className = testIdentifier.getSource().map(source -> { if (source instanceof ClassSource) { return ((ClassSource) source).getClassName(); } else if (source instanceof MethodSource) { return ((MethodSource) source).getClassName(); } return null; }).orElse(null); return new Description(testIdentifier.getDisplayName(), className); }
@Override public void executionStarted(TestIdentifier testIdentifier) { super.executionStarted(testIdentifier); if (testIdentifier.isTest()) { System.out.println(RUNNING_TEST + testIdentifier.getDisplayName()); } }
@Override public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { super.executionFinished(testIdentifier, testExecutionResult); if (testIdentifier.isTest()) { Status status = testExecutionResult.getStatus(); Color color = Color.valueOf(status); printMessage(color, STATUS + status); printFailureMessage(testIdentifier, testExecutionResult, color); System.out.println(); } }
@Override public void executionSkipped(TestIdentifier testIdentifier, String reason) { Map<String, Object> data = new HashMap<>(); data.put("type", "executionSkipped"); data.put("reason", reason); writeData(testIdentifier, data); }
@Override public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { Map<String, Object> data = new HashMap<>(); data.put("type", "executionFinished"); data.put("success", testExecutionResult.getStatus() == TestExecutionResult.Status.SUCCESSFUL); data.put("throwable", testExecutionResult.getThrowable().orElse(null)); writeData(testIdentifier, data); }
private void writeData(TestIdentifier id, Map<String, Object> data) { try { data.put("uniqueId", id.getUniqueId()); data.put("parentId", id.getParentId().orElse(null)); data.put("displayName", id.getDisplayName()); data.put("container", id.isContainer()); stream.writeObject(data); } catch (IOException e) { System.err.println("Could not write event: " + data); } }
@Override protected String getHistoryId(final TestIdentifier testIdentifier) { return null; }
private List<Label> getTags(final TestIdentifier testIdentifier) { return testIdentifier.getTags().stream() .map(tag -> new Label().withName(TAG).withValue(tag.getName())) .collect(Collectors.toList()); }
protected String getHistoryId(final TestIdentifier testIdentifier) { return md5(testIdentifier.getUniqueId()); }
@Override public void executionSkipped(TestIdentifier testIdentifier, String reason) { executionReportBuilder.addSkipped(testIdentifier, reason); }
@Override public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { executionReportBuilder.addResult(testIdentifier, testExecutionResult); }
public JUnit5TestUnit(TestIdentifier testIdentifier) { super(TestIdentifiers.toDescription(testIdentifier)); this.classLoaderDetection = IsolationUtils.loaderDetectionStrategy(); this.testIdentifier = testIdentifier; }
@Override public void testPlanExecutionFinished(TestPlan testPlan) { super.testPlanExecutionFinished(testPlan); long tests = testPlan.countTestIdentifiers(TestIdentifier::isTest); System.out.println("Test execution finished. Number of all tests: " + tests); }
private void printFailureMessage(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult, Color color) { if (testExecutionResult.getStatus().equals(Status.FAILED)) { testExecutionResult.getThrowable() .ifPresent(throwable -> printMessage(color, throwable.toString())); } }
@Override public void dynamicTestRegistered(TestIdentifier testIdentifier) { Map<String, Object> data = new HashMap<>(); data.put("type", "dynamicTestRegistered"); writeData(testIdentifier, data); }
@Override public void executionStarted(TestIdentifier testIdentifier) { Map<String, Object> data = new HashMap<>(); data.put("type", "executionStarted"); writeData(testIdentifier, data); }