Skip to content

Commit

Permalink
Merge pull request #50 from Xray-App/displaynamegeneration_fix
Browse files Browse the repository at this point in the history
fixes #49 by adding basic support for DisplayNameGeneration
  • Loading branch information
bitcoder authored Mar 23, 2024
2 parents 95a78b1 + 5cf8acc commit cf5d88b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>app.getxray</groupId>
<artifactId>xray-junit-extensions</artifactId>
<packaging>jar</packaging>
<version>0.7.1</version>
<version>0.7.2</version>
<name>xray-junit-extensions</name>
<description>Improvements for JUnit that allow you to take better advantage of JUnit 5 (jupiter engine) whenever using it together with Xray Test Management.</description>
<url>https://github.com/Xray-App/xray-junit-extensions</url>
Expand Down Expand Up @@ -262,7 +262,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<version>0.8.11</version>
<executions>
<execution>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package app.getxray.xray.junit.customjunitxml;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.TestFactory;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
Expand Down Expand Up @@ -233,6 +234,7 @@ private void writeTestcase(TestIdentifier testIdentifier, AggregatedTestResult t

final Optional<TestSource> testSource = testIdentifier.getSource();
final Optional<Method> testMethod = testSource.flatMap(this::getTestMethod);
final Class testClass = ((MethodSource)testSource.get()).getJavaClass();
Optional<XrayTest> xrayTest = AnnotationSupport.findAnnotation(testMethod, XrayTest.class);
Optional<Requirement> requirement = AnnotationSupport.findAnnotation(testMethod, Requirement.class);
if (reportOnlyAnnotatedTests && (!requirement.isPresent() && !xrayTest.isPresent())) {
Expand Down Expand Up @@ -301,13 +303,16 @@ private void writeTestcase(TestIdentifier testIdentifier, AggregatedTestResult t

Optional<TestFactory> dynamicTest = AnnotationSupport.findAnnotation(testMethod, TestFactory.class);
Optional<DisplayName> displayName = AnnotationSupport.findAnnotation(testMethod, DisplayName.class);
Optional<DisplayNameGeneration> displayNameGenerator = AnnotationSupport.findAnnotation(testClass, DisplayNameGeneration.class);

// this logic should be improved/simplified; the displayNameGererator logic isnt handling all cases, inclusing if it was set globally
if ( ((test_summary == null) || (test_summary.isEmpty())) && (displayName.isPresent()) ) {
//test_summary = testIdentifier.getDisplayName();
test_summary = displayName.get().value();
}
if ( ((test_summary == null) || (test_summary.isEmpty())) && (dynamicTest.isPresent()) ) {
if ( ((test_summary == null) || (test_summary.isEmpty())) && (dynamicTest.isPresent() || displayNameGenerator.isPresent()) ) {
test_summary = testIdentifier.getDisplayName();
}

if ((test_summary != null) && (!test_summary.isEmpty())) {
addProperty(writer, "test_summary", test_summary);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
public class EnhancedLegacyXmlTest {

private static final Class TEST_EXAMPLES_CLASS = XrayEnabledTestExamples.class;
private static final Class CUSTOM_DISPLAYNAME_CLASS = XrayEnabledTestCustomDisplayName.class;

private static final Runnable FAILING_BLOCK = () -> fail("should fail");
private static final Runnable SUCCEEDING_TEST = () -> {
};
Expand Down Expand Up @@ -257,6 +259,23 @@ public void simpleTestShouldNotHaveXrayProperties() throws Exception {
assertThat(testcase.child("properties").children("property").matchAttr("name", "requirements")).isEmpty();
}


@Test
public void simpleTestUsingCustomDisplayNameGeneration() throws Exception {
String testMethodName = "legacyTest";
executeTestMethod(CUSTOM_DISPLAYNAME_CLASS, testMethodName);

Match testsuite = readValidXmlFile(tempDirectory.resolve(REPORT_NAME));
Match testcase = testsuite.child("testcase");
assertThat(testcase.attr("name", String.class)).isEqualTo(testMethodName);
assertThat(testcase.child("properties").children("property").matchAttr("name", "test_id")).isEmpty();
assertThat(testcase.child("properties").children("property").matchAttr("name", "test_key")).isEmpty();
assertThat(testcase.child("properties").children("property").matchAttr("name", "test_summary").attr("value")).isEqualTo("legacy test (custom_name)");
assertThat(testcase.child("properties").children("property").matchAttr("name", "test_description")).isEmpty();
assertThat(testcase.child("properties").children("property").matchAttr("name", "tags")).isEmpty();
assertThat(testcase.child("properties").children("property").matchAttr("name", "requirements")).isEmpty();
}

@Test
public void testsShouldHaveStartAndFinishTimestamps() throws Exception {
String testMethodName = "legacyTest";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package app.getxray.xray.junit.customjunitxml;


import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayNameGeneration;

import java.lang.reflect.Method;
import org.junit.jupiter.api.DisplayNameGenerator.Standard;

@DisplayNameGeneration(XrayEnabledTestCustomDisplayName.CustomDisplayNameGenerator.class)
public class XrayEnabledTestCustomDisplayName {

@Test
public void legacyTest() {
}

public static class CustomDisplayNameGenerator extends Standard {

@Override
public String generateDisplayNameForClass(Class<?> testClass) {
return replaceCamelCase(replaceUndercoreBySpace(super.generateDisplayNameForClass(testClass)));
}

@Override
public String generateDisplayNameForNestedClass(Class<?> nestedClass) {
return replaceCamelCase(replaceUndercoreBySpace(super.generateDisplayNameForNestedClass(nestedClass)));
}

@Override
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) {
return this.replaceCamelCase(replaceUndercoreBySpace(testMethod.getName())) + " (custom_name)";
}

String replaceUndercoreBySpace(String s) {
return s.replace("_", " ");
}

String replaceCamelCase(String camelCase) {
StringBuilder result = new StringBuilder();
result.append(camelCase.charAt(0));
for (int i=1; i<camelCase.length(); i++) {
if (Character.isUpperCase(camelCase.charAt(i))) {
result.append(' ');
result.append(Character.toLowerCase(camelCase.charAt(i)));
} else {
result.append(camelCase.charAt(i));
}
}
return result.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import app.getxray.xray.junit.customjunitxml.XrayTestReporter;
import app.getxray.xray.junit.customjunitxml.XrayTestReporterParameterResolver;
import app.getxray.xray.junit.customjunitxml.annotations.Requirement;
import app.getxray.xray.junit.customjunitxml.annotations.XrayTest;

Expand Down

0 comments on commit cf5d88b

Please sign in to comment.