Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show the displayName of dynamic tests in the sbt output and in the xml test reports #94

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ private String toName(TestIdentifier identifier) {
name =
VINTAGE_ENGINE.equals(testEngine)
? toVintageName(identifier, lastSegment)
: toName(lastSegment);
: toName(identifier, lastSegment);
}

return name;
Expand All @@ -359,7 +359,7 @@ private String toName(TestIdentifier identifier) {
/*
* Formats a test segment from junit-jupiter.
*/
private String toName(Segment segment) {
private String toName(TestIdentifier identifier, Segment segment) {

String name;

Expand All @@ -377,7 +377,7 @@ private String toName(Segment segment) {
name = colorTheme.testFactory().format("#" + segment.getValue());
break;
case "dynamic-test":
name = colorTheme.dynamicTest().format(":" + segment.getValue());
name = colorTheme.dynamicTest().format(":" + identifier.getDisplayName());
break;
case "test-template":
name = colorTheme.testTemplate().format("#" + segment.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static TaskName of(String testSuite, TestIdentifier identifier) {

MethodSource methodSource = (MethodSource) testSource;
result.nestedSuiteId = nestedSuiteId(testSuite, methodSource.getClassName());
result.invocation = invocation(UniqueId.parse(identifier.getUniqueId()));
result.invocation = invocation(identifier, UniqueId.parse(identifier.getUniqueId()));
result.testName =
testName(methodSource.getMethodName(), methodSource.getMethodParameterTypes());
}
Expand Down Expand Up @@ -159,7 +159,7 @@ static String testName(String methodName, String methodParameterTypes) {
* @param id The unique test identifier.
* @return A string representation of the current invocation (might be {@code null}).
*/
static String invocation(UniqueId id) {
static String invocation(TestIdentifier identifier, UniqueId id) {

List<UniqueId.Segment> segments = id.getSegments();

Expand All @@ -169,6 +169,7 @@ static String invocation(UniqueId id) {

switch (last.getType()) {
case "dynamic-test":
return identifier.getDisplayName();
case "test-template-invocation":
return last.getValue().replace("#", "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@

import static org.junit.jupiter.api.DynamicTest.dynamicTest;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

Expand All @@ -45,7 +46,11 @@ static class DynamicTests {

@TestFactory
Collection<DynamicTest> test() {
return Collections.singletonList(dynamicTest("1st dynamic test", () -> {}));
final Executable dummy = () -> {};
return Arrays.asList(
dynamicTest("1st dynamic test", dummy),
dynamicTest("2nd dynamic test", dummy),
dynamicTest("3rd dynamic test", dummy));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.github.sbt.junit.jupiter.internal.event.DispatcherSampleTests.NestedTests;
import com.github.sbt.junit.jupiter.internal.event.DispatcherSampleTests.ParameterizedTests;
import com.github.sbt.junit.jupiter.internal.event.DispatcherSampleTests.SingleParamTests;
import java.util.Arrays;
import java.util.List;
import junit.TestRunner;
import org.hamcrest.FeatureMatcher;
Expand Down Expand Up @@ -121,12 +122,16 @@ public void shouldReportDynamicTests() {
List<Event> result = testRunner.eventHandler().byStatus(Status.Success);

String suiteName = ".event.DispatcherSampleTests$DynamicTests";
String testName = "test():1";
List<String> testNames =
Arrays.asList(
"test():1st dynamic test", "test():2nd dynamic test", "test():3rd dynamic test");

assertThat(result, hasSize(1));
assertThat(result, hasSize(3));
assertThat(result, hasItem(fullyQualifiedName(endsWith(suiteName))));
assertThat(result, hasItem(selector(instanceOf(TestSelector.class))));
assertThat(result, hasItem(selector(testName(equalTo(testName)))));
for (String testName : testNames) {
assertThat(result, hasItem(selector(testName(equalTo(testName)))));
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@

import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.descriptor.AbstractTestDescriptor;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.runner.RunWith;

/** @author Michael Aichler */
Expand Down Expand Up @@ -98,28 +101,45 @@ public static class InvocationTest {
public void shouldFindDynamicTest() {

UniqueId id = UniqueId.root("method", "someTestMethod").append("dynamic-test", "#1");
TestDescriptor descriptor = new DummyTestDescriptor(id, "myDynamicTest");
TestIdentifier identifier = TestIdentifier.from(descriptor);

String result = TaskName.invocation(id);
assertThat(result, equalTo("1"));
String result = TaskName.invocation(identifier, id);
assertThat(result, equalTo("myDynamicTest"));
}

@Test
public void shouldFindTestTemplateInvocation() {

UniqueId id =
UniqueId.root("method", "someTestMethod").append("test-template-invocation", "#1");
TestDescriptor descriptor = new DummyTestDescriptor(id, "myTestTemplateInvocation");
TestIdentifier identifier = TestIdentifier.from(descriptor);

String result = TaskName.invocation(id);
String result = TaskName.invocation(identifier, id);
assertThat(result, equalTo("1"));
}

@Test
public void shouldReturnNullOtherwise() {

UniqueId id = UniqueId.root("method", "someTestMethod");
TestDescriptor descriptor = new DummyTestDescriptor(id, "someTestMethod");
TestIdentifier identifier = TestIdentifier.from(descriptor);

String result = TaskName.invocation(id);
String result = TaskName.invocation(identifier, id);
assertThat(result, nullValue());
}
}

private static class DummyTestDescriptor extends AbstractTestDescriptor {
protected DummyTestDescriptor(UniqueId uniqueId, String displayName) {
super(uniqueId, displayName);
}

@Override
public Type getType() {
return Type.CONTAINER_AND_TEST;
}
}
}