From ddc7230a0a1ff7530c07c202fd48f666aad2f49d Mon Sep 17 00:00:00 2001 From: mstepachev Date: Fri, 12 Apr 2024 15:10:50 +0100 Subject: [PATCH 1/7] #1032 Inherited test methods don't support their annotations. --- .../AllureJunitPlatformUtils.java | 12 ++++- .../AllureJunitPlatformTest.java | 40 ++++++++++++++ .../features/InheritedTests.java | 54 +++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/InheritedTests.java diff --git a/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatformUtils.java b/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatformUtils.java index 14769ed2..4703a5b4 100644 --- a/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatformUtils.java +++ b/allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatformUtils.java @@ -86,12 +86,20 @@ public static Optional getTestMethod(final TestSource source) { public static Optional getTestMethod(final MethodSource source) { try { final Class aClass = Class.forName(source.getClassName()); - return Stream.of(aClass.getDeclaredMethods()) - .filter(method -> MethodSource.from(method).equals(source)) + + return Stream.of(aClass.getMethods()) + // The filter ignores the class name, as the methods are already defined in an inherited class. + .filter(method -> equalsWithoutClassName(MethodSource.from(method), source)) .findAny(); } catch (ClassNotFoundException e) { LOGGER.trace("Could not get test method from method source {}", source, e); } return Optional.empty(); } + + private static boolean equalsWithoutClassName(MethodSource a, MethodSource b) { + return a.getMethodName().equals(b.getMethodName()) + && a.getMethodParameterTypes().equals(b.getMethodParameterTypes()); + + } } 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 9e9c1b82..73ee6547 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 @@ -26,6 +26,7 @@ import io.qameta.allure.junitplatform.features.DisabledTests; import io.qameta.allure.junitplatform.features.DynamicTests; import io.qameta.allure.junitplatform.features.FailedTests; +import io.qameta.allure.junitplatform.features.InheritedTests; import io.qameta.allure.junitplatform.features.JupiterUniqueIdTest; import io.qameta.allure.junitplatform.features.MarkerAnnotationSupport; import io.qameta.allure.junitplatform.features.NestedTests; @@ -75,6 +76,7 @@ import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; import org.junit.platform.launcher.core.LauncherFactory; +import java.lang.annotation.Inherited; import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.List; @@ -874,6 +876,44 @@ void shouldSetDifferentUuidsInDifferentRuns() { } + @Test + void shouldInheritedTestAnnotations() { + final AllureResults allureResults = runClasses(InheritedTests.class); + + TestResult grandparentTest = allureResults.getTestResultByName("grandparentTest()"); + assertThat(grandparentTest.getLabels()) + .extracting(Label::getName, Label::getValue) + .contains( + tuple("epic", InheritedTests.INHERITED_TEST_EPIC), + tuple("feature", InheritedTests.INHERITED_TEST_FUTURE), + tuple("story", InheritedTests.INHERITED_TEST_GRANDPARENT_STORY) + ); + assertThat(grandparentTest.getDescription()).isEqualTo(InheritedTests.TEST_DESCRIPTION); + assertThat(grandparentTest.getLinks()).extracting(Link::getName).contains(InheritedTests.TEST_LINK); + + TestResult parentTest = allureResults.getTestResultByName("parentTest()"); + assertThat(parentTest.getLabels()) + .extracting(Label::getName, Label::getValue) + .contains( + tuple("epic", InheritedTests.INHERITED_TEST_EPIC), + tuple("feature", InheritedTests.INHERITED_TEST_FUTURE), + tuple("story", InheritedTests.INHERITED_TEST_PARENT_STORY) + ); + assertThat(parentTest.getDescription()).isEqualTo(InheritedTests.TEST_DESCRIPTION); + assertThat(parentTest.getLinks()).extracting(Link::getName).contains(InheritedTests.TEST_LINK); + + TestResult childTest = allureResults.getTestResultByName("childTest()"); + assertThat(childTest.getLabels()) + .extracting(Label::getName, Label::getValue) + .contains( + tuple("epic", InheritedTests.INHERITED_TEST_EPIC), + tuple("feature", InheritedTests.INHERITED_TEST_FUTURE), + tuple("story", InheritedTests.INHERITED_TEST_CHILD_STORY) + ); + assertThat(childTest.getDescription()).isEqualTo(InheritedTests.TEST_DESCRIPTION); + assertThat(childTest.getLinks()).extracting(Link::getName).contains(InheritedTests.TEST_LINK); + } + @Test void shouldSupportNestedClasses() { final AllureResults allureResults = runClasses(NestedTests.class); diff --git a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/InheritedTests.java b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/InheritedTests.java new file mode 100644 index 00000000..4f7e1281 --- /dev/null +++ b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/InheritedTests.java @@ -0,0 +1,54 @@ +package io.qameta.allure.junitplatform.features; + +import io.qameta.allure.Description; +import io.qameta.allure.Epic; +import io.qameta.allure.Feature; +import io.qameta.allure.Link; +import io.qameta.allure.Story; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +public class InheritedTests { + public static final String INHERITED_TEST_EPIC = "Inherited epic"; + public static final String INHERITED_TEST_FUTURE = "Inherited future"; + public static final String INHERITED_TEST_GRANDPARENT_STORY = "Inherited grandparent story"; + + public static final String INHERITED_TEST_PARENT_STORY = "Inherited parent story"; + + public static final String INHERITED_TEST_CHILD_STORY = "Inherited child story"; + + public static final String TEST_DESCRIPTION = "Test description"; + + public static final String TEST_LINK = "Test link"; + + @Epic(INHERITED_TEST_EPIC) + @Feature(INHERITED_TEST_FUTURE) + public abstract static class GrandparentTest { + @Test + @Description(TEST_DESCRIPTION) + @Story(INHERITED_TEST_GRANDPARENT_STORY) + @Link(TEST_LINK) + public void grandparentTest() { + } + } + + public abstract static class ParentTest extends GrandparentTest { + @Test + @Description(TEST_DESCRIPTION) + @Story(INHERITED_TEST_PARENT_STORY) + @Link(TEST_LINK) + public void parentTest() { + } + + } + + @Nested + class ChildTest extends ParentTest { + @Test + @Description(TEST_DESCRIPTION) + @Story(INHERITED_TEST_CHILD_STORY) + @Link(TEST_LINK) + public void childTest() { + } + } +} From eafa3fb1966c2de811b06f120db038b7f42a27fc Mon Sep 17 00:00:00 2001 From: mstepachev Date: Fri, 12 Apr 2024 15:39:28 +0100 Subject: [PATCH 2/7] spotlessApply --- .../junitplatform/AllureJunitPlatformTest.java | 1 - .../junitplatform/features/InheritedTests.java | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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 73ee6547..03954676 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 @@ -76,7 +76,6 @@ import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; import org.junit.platform.launcher.core.LauncherFactory; -import java.lang.annotation.Inherited; import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.List; diff --git a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/InheritedTests.java b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/InheritedTests.java index 4f7e1281..2324b6bd 100644 --- a/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/InheritedTests.java +++ b/allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/features/InheritedTests.java @@ -1,3 +1,18 @@ +/* + * Copyright 2016-2024 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 io.qameta.allure.Description; From 0c8d4583a89b77148d779a9288381b742956c103 Mon Sep 17 00:00:00 2001 From: mstepachev Date: Fri, 12 Apr 2024 17:50:29 +0100 Subject: [PATCH 3/7] More deeps introspect. --- .../qameta/allure/util/AnnotationUtils.java | 3 +- .../qameta/allure/util/ReflectionUtils.java | 102 ++++++++++++++++++ .../AllureJunitPlatformUtils.java | 5 +- .../features/InheritedTests.java | 10 +- 4 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 allure-java-commons/src/main/java/io/qameta/allure/util/ReflectionUtils.java diff --git a/allure-java-commons/src/main/java/io/qameta/allure/util/AnnotationUtils.java b/allure-java-commons/src/main/java/io/qameta/allure/util/AnnotationUtils.java index d241a468..dc0b10a0 100644 --- a/allure-java-commons/src/main/java/io/qameta/allure/util/AnnotationUtils.java +++ b/allure-java-commons/src/main/java/io/qameta/allure/util/AnnotationUtils.java @@ -37,6 +37,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static io.qameta.allure.util.ReflectionUtils.getAllClassAnnotations; import static io.qameta.allure.util.ResultsUtils.createLabel; import static io.qameta.allure.util.ResultsUtils.createLink; import static java.util.Arrays.asList; @@ -116,7 +117,7 @@ public static Set getLinks(final Collection annotations) { * @return discovered labels. */ public static Set