Skip to content

Commit f444a8d

Browse files
committed
[Allure_2 + JUnit_5] Added applying:
- @description for test methods - @DisplayName for test classes
1 parent 223b9d3 commit f444a8d

6 files changed

Lines changed: 122 additions & 1 deletion

File tree

allure-junit-platform/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies {
1414

1515
compile project(':allure-java-commons')
1616
compile('org.junit.platform:junit-platform-launcher')
17+
compile('org.junit.jupiter:junit-jupiter-api')
1718

1819
testCompile('org.slf4j:slf4j-simple')
1920
testCompile 'org.assertj:assertj-core'

allure-junit-platform/src/main/java/io/qameta/allure/junit5/AllureJunit5.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
import io.qameta.allure.Allure;
44
import io.qameta.allure.AllureLifecycle;
5+
import io.qameta.allure.Description;
56
import io.qameta.allure.model.Label;
67
import io.qameta.allure.model.Stage;
78
import io.qameta.allure.model.Status;
89
import io.qameta.allure.model.TestResult;
910
import io.qameta.allure.util.ResultsUtils;
11+
import org.junit.jupiter.api.DisplayName;
1012
import org.junit.platform.engine.TestExecutionResult;
1113
import org.junit.platform.engine.support.descriptor.MethodSource;
1214
import org.junit.platform.launcher.TestExecutionListener;
1315
import org.junit.platform.launcher.TestIdentifier;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
1418

1519
import java.math.BigInteger;
1620
import java.security.MessageDigest;
@@ -30,6 +34,8 @@
3034
*/
3135
public class AllureJunit5 implements TestExecutionListener {
3236

37+
private static final Logger LOGGER = LoggerFactory.getLogger(AllureJunit5.class);
38+
3339
private static final String TAG = "tag";
3440

3541

@@ -65,7 +71,8 @@ public void executionStarted(final TestIdentifier testIdentifier) {
6571
.withStage(Stage.RUNNING);
6672

6773
methodSource.ifPresent(source -> {
68-
result.getLabels().add(new Label().withName("suite").withValue(source.getClassName()));
74+
result.setDescription(getDescription(source));
75+
result.getLabels().add(new Label().withName("suite").withValue(getSuite(source)));
6976
result.getLabels().add(new Label().withName("package").withValue(source.getClassName()));
7077
});
7178

@@ -130,4 +137,31 @@ private MessageDigest getMessageDigest() {
130137
throw new IllegalStateException("Could not find md5 hashing algorithm", e);
131138
}
132139
}
140+
141+
private String getSuite(final MethodSource source) {
142+
try {
143+
final DisplayName displayNameAnnotation =
144+
Class.forName(source.getClassName()).getAnnotation(DisplayName.class);
145+
if (displayNameAnnotation != null && !displayNameAnnotation.value().isEmpty()) {
146+
return displayNameAnnotation.value();
147+
}
148+
} catch (ClassNotFoundException e) {
149+
LOGGER.trace(e.getMessage(), e);
150+
}
151+
return source.getClassName();
152+
}
153+
154+
private String getDescription(final MethodSource source) {
155+
try {
156+
final Description descriptionAnnotation = Class.forName(source.getClassName())
157+
.getDeclaredMethod(source.getMethodName())
158+
.getAnnotation(Description.class);
159+
if (descriptionAnnotation != null) {
160+
return descriptionAnnotation.value();
161+
}
162+
} catch (ClassNotFoundException | NoSuchMethodException e) {
163+
LOGGER.trace(e.getMessage(), e);
164+
}
165+
return null;
166+
}
133167
}

allure-junit-platform/src/test/java/io/qameta/allure/junit5/AllureJunit5Test.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,49 @@ void shouldAddTags() {
208208
softly.assertAll();
209209
}
210210

211+
@Test
212+
void shouldProcessTestClassDisplayNameByAnnotation() {
213+
runClasses(TestsClassWithDisplayNameAnnotation.class);
214+
215+
final List<TestResult> testResults = results.getTestResults();
216+
assertThat(testResults)
217+
.hasSize(1);
218+
219+
final List<Label> testResultLabels = testResults.get(0).getLabels();
220+
assertThat(testResultLabels)
221+
.filteredOn(label -> "suite".equals(label.getName()))
222+
.hasSize(1)
223+
.flatExtracting(Label::getValue)
224+
.contains("Display name of test class");
225+
}
226+
227+
@Test
228+
void shouldProcessDefaultTestClassDisplayName() {
229+
runClasses(TestsClassWithoutDisplayNameAnnotation.class);
230+
231+
final List<TestResult> testResults = results.getTestResults();
232+
assertThat(testResults)
233+
.hasSize(1);
234+
235+
final List<Label> testResultLabels = testResults.get(0).getLabels();
236+
assertThat(testResultLabels)
237+
.filteredOn(label -> "suite".equals(label.getName()))
238+
.hasSize(1)
239+
.flatExtracting(Label::getValue)
240+
.contains("io.qameta.allure.junit5.features.TestsClassWithoutDisplayNameAnnotation");
241+
}
242+
243+
@Test
244+
void shouldProcessJunit5Description() {
245+
runClasses(TestsWithDescriptions.class);
246+
247+
final List<TestResult> testResults = results.getTestResults();
248+
assertThat(testResults)
249+
.hasSize(1)
250+
.flatExtracting(TestResult::getDescription)
251+
.contains("Test description");
252+
}
253+
211254
private void runClasses(Class<?>... classes) {
212255
final ClassSelector[] classSelectors = Stream.of(classes)
213256
.map(DiscoverySelectors::selectClass)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.qameta.allure.junit5.features;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
/**
7+
* @author a.afrikanov (Andrey Afrikanov).
8+
*/
9+
@DisplayName("Display name of test class")
10+
public class TestsClassWithDisplayNameAnnotation {
11+
12+
@Test
13+
void test() {
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.qameta.allure.junit5.features;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
/**
6+
* @author a.afrikanov (Andrey Afrikanov).
7+
*/
8+
public class TestsClassWithoutDisplayNameAnnotation {
9+
10+
@Test
11+
void test() {
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.qameta.allure.junit5.features;
2+
3+
import io.qameta.allure.Description;
4+
import org.junit.jupiter.api.Test;
5+
6+
/**
7+
* @author a.afrikanov (Andrey Afrikanov).
8+
*/
9+
public class TestsWithDescriptions {
10+
11+
@Test
12+
@Description("Test description")
13+
void testWithDescription() {
14+
}
15+
}

0 commit comments

Comments
 (0)