Skip to content

Commit 5591a12

Browse files
authored
feat(junit-platform): add annotations from outer classes (fixes #391, via #1019)
1 parent 1f490e2 commit 5591a12

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

allure-junit-platform/src/main/java/io/qameta/allure/junitplatform/AllureJunitPlatform.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,16 @@ private void startTestCase(final TestIdentifier testIdentifier) {
553553

554554
result.getLabels().add(getJUnitPlatformUniqueId(testIdentifier));
555555

556-
testClass.map(AnnotationUtils::getLabels).ifPresent(result.getLabels()::addAll);
556+
// add annotations from outer classes (support for @Nested tests in JUnit 5)
557+
testClass.ifPresent(clazz -> {
558+
Class<?> clazz1 = clazz;
559+
do {
560+
final Set<Label> labels = AnnotationUtils.getLabels(clazz1);
561+
result.getLabels().addAll(labels);
562+
clazz1 = clazz1.getDeclaringClass();
563+
} while (Objects.nonNull(clazz1));
564+
});
565+
557566
testMethod.map(AnnotationUtils::getLabels).ifPresent(result.getLabels()::addAll);
558567

559568
testClass.map(AnnotationUtils::getLinks).ifPresent(result.getLinks()::addAll);

allure-junit-platform/src/test/java/io/qameta/allure/junitplatform/AllureJunitPlatformTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.qameta.allure.junitplatform.features.FailedTests;
2929
import io.qameta.allure.junitplatform.features.JupiterUniqueIdTest;
3030
import io.qameta.allure.junitplatform.features.MarkerAnnotationSupport;
31+
import io.qameta.allure.junitplatform.features.NestedTests;
3132
import io.qameta.allure.junitplatform.features.OneTest;
3233
import io.qameta.allure.junitplatform.features.OwnerTest;
3334
import io.qameta.allure.junitplatform.features.ParallelTests;
@@ -873,4 +874,54 @@ void shouldSetDifferentUuidsInDifferentRuns() {
873874

874875
}
875876

877+
@Test
878+
void shouldSupportNestedClasses() {
879+
final AllureResults allureResults = runClasses(NestedTests.class);
880+
881+
assertThat(allureResults.getTestResults())
882+
.extracting(TestResult::getName)
883+
.containsExactlyInAnyOrder(
884+
"parentTest()",
885+
"feature1Test()",
886+
"feature2Test()",
887+
"story1Test()"
888+
);
889+
890+
assertThat(allureResults.getTestResults())
891+
.filteredOn("name", "parentTest()")
892+
.flatExtracting(TestResult::getLabels)
893+
.extracting(Label::getName, Label::getValue)
894+
.contains(
895+
tuple("epic", "Parent epic"),
896+
tuple("feature", "Parent feature")
897+
);
898+
899+
assertThat(allureResults.getTestResults())
900+
.filteredOn("name", "feature1Test()")
901+
.flatExtracting(TestResult::getLabels)
902+
.extracting(Label::getName, Label::getValue)
903+
.contains(
904+
tuple("epic", "Parent epic"),
905+
tuple("feature", "Feature 1")
906+
);
907+
908+
assertThat(allureResults.getTestResults())
909+
.filteredOn("name", "feature2Test()")
910+
.flatExtracting(TestResult::getLabels)
911+
.extracting(Label::getName, Label::getValue)
912+
.contains(
913+
tuple("epic", "Parent epic"),
914+
tuple("feature", "Feature 2")
915+
);
916+
917+
assertThat(allureResults.getTestResults())
918+
.filteredOn("name", "story1Test()")
919+
.flatExtracting(TestResult::getLabels)
920+
.extracting(Label::getName, Label::getValue)
921+
.contains(
922+
tuple("epic", "Parent epic"),
923+
tuple("feature", "Feature 2"),
924+
tuple("story", "Story 1")
925+
);
926+
}
876927
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2016-2024 Qameta Software Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.qameta.allure.junitplatform.features;
17+
18+
import io.qameta.allure.Epic;
19+
import io.qameta.allure.Feature;
20+
import io.qameta.allure.Story;
21+
import org.junit.jupiter.api.Nested;
22+
import org.junit.jupiter.api.Test;
23+
24+
/**
25+
* @author charlie (Dmitry Baev).
26+
*/
27+
@Epic("Parent epic")
28+
public class NestedTests {
29+
30+
@Feature("Parent feature")
31+
@Test
32+
void parentTest() {
33+
}
34+
35+
@Feature("Feature 1")
36+
@Nested
37+
class Feature1 {
38+
39+
@Test
40+
void feature1Test() {
41+
}
42+
}
43+
44+
@Feature("Feature 2")
45+
@Nested
46+
class Feature2 {
47+
48+
@Test
49+
void feature2Test() {
50+
}
51+
52+
@Story("Story 1")
53+
@Nested
54+
class Story1 {
55+
56+
@Test
57+
void story1Test() {
58+
}
59+
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)