From f889cbb0161612a6baab5da8cc59e2bcd4d680ba Mon Sep 17 00:00:00 2001 From: Dmitry Baev Date: Fri, 31 Jul 2026 13:46:36 +0100 Subject: [PATCH] Avoid duplicate display names for JUnit Platform test templates --- .../junitplatform/AllureJunitPlatform.java | 15 +++++++--- .../AllureJunitPlatformTest.java | 29 ++++++++++++++++++ .../RepeatedTestsWithDisplayName.java | 30 +++++++++++++++++++ .../RepeatedTestsWithLongDisplayName.java | 30 +++++++++++++++++++ 4 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/RepeatedTestsWithDisplayName.java create mode 100644 allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/RepeatedTestsWithLongDisplayName.java diff --git a/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java b/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java index 0f9b652c..90a9a39c 100644 --- a/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java +++ b/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java @@ -503,9 +503,10 @@ private List getParentScopeKeys(final TestIdentifier testIden private String getName(final TestIdentifier testIdentifier, final boolean testTemplate, final Optional maybeParent) { - final String baseName = testTemplate && maybeParent.isPresent() - ? maybeParent.get().getDisplayName() + " " + testIdentifier.getDisplayName() - : testIdentifier.getDisplayName(); + final String parentPrefix = testTemplate + ? maybeParent.map(TestIdentifier::getDisplayName).orElse("") + : ""; + final String baseName = prependIfMissing(parentPrefix, testIdentifier.getDisplayName()); // prefix the name with parameterized class invocation display names, so results // of the same method from different invocations are distinguishable final String prefix = getParents(testIdentifier).stream() @@ -515,7 +516,13 @@ private String getName(final TestIdentifier testIdentifier, ) .map(TestIdentifier::getDisplayName) .collect(Collectors.joining(" ")); - return prefix.isEmpty() ? baseName : prefix + " " + baseName; + return prependIfMissing(prefix, baseName); + } + + private static String prependIfMissing(final String prefix, final String name) { + return prefix.isEmpty() || name.startsWith(prefix) + ? name + : prefix + " " + name; } /** diff --git a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTest.java b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTest.java index 275a5833..a030ea8c 100644 --- a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTest.java +++ b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTest.java @@ -43,6 +43,8 @@ import io.qameta.allure.junitplatform.features.ParameterisedTestsWithDisplayName; import io.qameta.allure.junitplatform.features.PassedTests; import io.qameta.allure.junitplatform.features.RepeatedTests; +import io.qameta.allure.junitplatform.features.RepeatedTestsWithDisplayName; +import io.qameta.allure.junitplatform.features.RepeatedTestsWithLongDisplayName; import io.qameta.allure.junitplatform.features.ReportEntryParameter; import io.qameta.allure.junitplatform.features.RuntimeParametersTest; import io.qameta.allure.junitplatform.features.RuntimeSuiteLabelTest; @@ -1040,6 +1042,33 @@ void shouldSupportRepeatedTests() { } + @Issue("1122") + @AllureFeatures.DisplayName + @Test + void shouldNotDuplicateDisplayNameForRepeatedTests() { + final AllureResults results = runClasses(RepeatedTestsWithDisplayName.class); + final List testResults = results.getTestResults(); + + assertThat(testResults) + .extracting(TestResult::getName) + .containsExactly("Should work as expected [Attempt 1]"); + } + + @Issue("831") + @AllureFeatures.DisplayName + @Test + void shouldNotDuplicateLongDisplayNameForRepeatedTests() { + final AllureResults results = runClasses(RepeatedTestsWithLongDisplayName.class); + final List testResults = results.getTestResults(); + + assertThat(testResults) + .extracting(TestResult::getName) + .containsExactly( + "Long name :: repetition 1 of 2", + "Long name :: repetition 2 of 2" + ); + } + @AllureFeatures.MarkerAnnotations @Test void shouldSupportDisabledRepeatedTests() { diff --git a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/RepeatedTestsWithDisplayName.java b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/RepeatedTestsWithDisplayName.java new file mode 100644 index 00000000..44b61d92 --- /dev/null +++ b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/RepeatedTestsWithDisplayName.java @@ -0,0 +1,30 @@ +/* + * Copyright 2016-2026 Qameta Software Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.qameta.allure.junitplatform.features; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.RepeatedTest; + +public class RepeatedTestsWithDisplayName { + + @DisplayName("Should work as expected") + @RepeatedTest( + value = 1, + name = "{displayName} [Attempt {currentRepetition}]" + ) + void shouldWorkAsExpected() { + } +} diff --git a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/RepeatedTestsWithLongDisplayName.java b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/RepeatedTestsWithLongDisplayName.java new file mode 100644 index 00000000..e12d81ba --- /dev/null +++ b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/RepeatedTestsWithLongDisplayName.java @@ -0,0 +1,30 @@ +/* + * Copyright 2016-2026 Qameta Software Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.qameta.allure.junitplatform.features; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.RepeatedTest; + +public class RepeatedTestsWithLongDisplayName { + + @DisplayName("Long name") + @RepeatedTest( + value = 2, + name = RepeatedTest.LONG_DISPLAY_NAME + ) + void repeatLong() { + } +}