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

Inherited test methods don't support their annotations (fixes #1032) #1033

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,20 @@ public static Optional<Method> getTestMethod(final TestSource source) {
public static Optional<Method> 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());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
}
}
}
Loading