Skip to content

EngineTestKit started Executions same as finished #5525

@tonyhallett

Description

@tonyhallett

Given that there is only Execution.skipped and Execution.finished

private static List<Execution> createExecutions(List<Event> events) {

	private static List<Execution> createExecutions(List<Event> events) {
		List<Execution> executions = new ArrayList<>();
		Map<TestDescriptor, Instant> executionStarts = new HashMap<>();

		for (Event event : events) {
			switch (event.getType()) {
				case STARTED -> executionStarts.put(event.getTestDescriptor(), event.getTimestamp());
				case SKIPPED -> {
					// Based on the Javadoc for EngineExecutionListener.executionSkipped(...),
					// a skipped descriptor must never be reported as started or finished,
					// but just in case a TestEngine does not adhere to that contract, we
					// make an attempt to remove any tracked "started" event.
					executionStarts.remove(event.getTestDescriptor());

					// Use the same timestamp for both the start and end Instant values.
					Instant timestamp = event.getTimestamp();

					Execution skippedEvent = Execution.skipped(event.getTestDescriptor(), timestamp, timestamp,
						event.getRequiredPayload(String.class));
					executions.add(skippedEvent);
				}
				case FINISHED -> {
					Instant startInstant = executionStarts.remove(event.getTestDescriptor());
					Instant endInstant = event.getTimestamp();

					// If "started" events have already been filtered out, it is no
					// longer possible to create a legitimate Execution for the current
					// descriptor. So we effectively ignore it in that case.
					if (startInstant != null) {
						Execution finishedEvent = Execution.finished(event.getTestDescriptor(), startInstant,
							endInstant, event.getRequiredPayload(TestExecutionResult.class));
						executions.add(finishedEvent);
					}
				}
				default -> {
					// Ignore other events
				}
			}
		}

		return executions;
	}

and skipped and finished have associated TerminationInfo

https://github.com/junit-team/junit-framework/blob/main/junit-platform-testkit/src/main/java/org/junit/platform/testkit/engine/Execution.java

	public static Execution finished(TestDescriptor testDescriptor, Instant startInstant, Instant endInstant,
			TestExecutionResult executionResult) {

		return new Execution(testDescriptor, startInstant, endInstant, TerminationInfo.executed(executionResult));
	}

	public static Execution skipped(TestDescriptor testDescriptor, Instant startInstant, Instant endInstant,
			String skipReason) {

		return new Execution(testDescriptor, startInstant, endInstant, TerminationInfo.skipped(skipReason));
	}

https://github.com/junit-team/junit-framework/blob/main/junit-platform-testkit/src/main/java/org/junit/platform/testkit/engine/TerminationInfo.java

	public static TerminationInfo skipped(@Nullable String reason) {
		return new TerminationInfo(true, reason, null);
	}

	public static TerminationInfo executed(TestExecutionResult testExecutionResult) {
		Preconditions.notNull(testExecutionResult, "TestExecutionResult must not be null");
		return new TerminationInfo(false, null, testExecutionResult);
	}
	private TerminationInfo(boolean skipped, @Nullable String skipReason,
			@Nullable TestExecutionResult testExecutionResult) {
		boolean executed = (testExecutionResult != null);
		Preconditions.condition((skipped ^ executed),
			"TerminationInfo must represent either a skipped execution or a TestExecutionResult but not both");

		this.skipped = skipped;
		this.skipReason = skipReason;
		this.testExecutionResult = testExecutionResult;
	}

and started are those that have not been skipped

	public Executions started() {
		return new Executions(executionsByTerminationInfo(TerminationInfo::notSkipped), this.category + " Started");
	}
	private Stream<Execution> executionsByTerminationInfo(Predicate<TerminationInfo> predicate) {
		return filter(execution -> predicate.test(execution.getTerminationInfo()));
	}

then this will be all executions that have finished, given that they were not skipped.

So there is no way to determine if the engine has invoked executionStarted but has failed to follow that up with executionSkipped or executionFinished.

The associated test

assertThat(testEvents.executions().started().count()).isEqualTo(3);

which uses the well behaved junit-jupiter, asserting started ( "finished").

Then started is all those executions that were created

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions