Skip to content

Commit

Permalink
add test for reports per test class and fixed logic for name of report
Browse files Browse the repository at this point in the history
  • Loading branch information
bitcoder committed Apr 2, 2024
1 parent 3aa3bfe commit da1b57a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 6 deletions.
2 changes: 1 addition & 1 deletion 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.3</version>
<version>0.8.0</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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class EnhancedLegacyXmlReportGeneratingListener implements TestExecutionL
private String reportFilename = null;
boolean addTimestampToReportFilename = false;
boolean reportOnlyAnnotatedTests = false;
boolean reportsPerClass = false;

private XmlReportData reportData;

Expand Down Expand Up @@ -93,6 +94,7 @@ public EnhancedLegacyXmlReportGeneratingListener(Path reportsDir, Path propertie
}
this.addTimestampToReportFilename = "true".equals(properties.getProperty("add_timestamp_to_report_filename"));
this.reportOnlyAnnotatedTests = "true".equals(properties.getProperty("report_only_annotated_tests", "false"));
this.reportsPerClass = "true".equals(properties.getProperty("reports_per_class", "false"));
} else {
if (reportsDir == null) {
this.reportsDir = FileSystems.getDefault().getPath(DEFAULT_REPORTS_DIR);
Expand Down Expand Up @@ -155,7 +157,12 @@ public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult

private void writeXmlReportInCaseOfRoot(TestIdentifier testIdentifier) {
if (isRoot(testIdentifier)) {
String rootName = UniqueId.parse(testIdentifier.getUniqueId()).getSegments().get(0).getValue();
String rootName;
if (reportsPerClass) {
rootName = testIdentifier.getLegacyReportingName();
} else {
rootName = UniqueId.parse(testIdentifier.getUniqueId()).getSegments().get(0).getValue();
}
writeXmlReportSafely(testIdentifier, rootName);
/*
Expand Down Expand Up @@ -192,8 +199,11 @@ private void writeXmlReportSafely(TestIdentifier testIdentifier, String rootName
}

private boolean isRoot(TestIdentifier testIdentifier) {
return !testIdentifier.getParentId().isPresent();
//return testIdentifier.getParentId().isPresent() && testIdentifier.getParentIdObject().get().getSegments().size() == 1;
if (reportsPerClass) {
return testIdentifier.getParentId().isPresent() && testIdentifier.getParentIdObject().get().getSegments().size() == 1;
} else {
return !testIdentifier.getParentId().isPresent();
}
}

private void printException(String message, Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package app.getxray.xray.junit.customjunitxml;

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

public class BasicTestExample {

@Test
@Order(1)
public void someBasicTest() {
}

@Test
@Order(2)
public void anotherBasicTest() {
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.nio.channels.Selector;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -40,6 +41,7 @@
import java.time.ZonedDateTime;
import java.util.Base64;
import java.util.Properties;
import java.util.stream.Collectors;

import javax.xml.XMLConstants;
import javax.xml.transform.dom.DOMSource;
Expand All @@ -52,6 +54,7 @@
//import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.engine.reporting.ReportEntry;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
Expand All @@ -63,12 +66,14 @@
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import app.getxray.xray.junit.customjunitxml.EnhancedLegacyXmlReportGeneratingListener;

public class EnhancedLegacyXmlTest {

private static final Class TEST_EXAMPLES_CLASS = XrayEnabledTestExamples.class;
private static final Class CUSTOM_DISPLAYNAME_CLASS = XrayEnabledTestCustomDisplayName.class;
private static final Class BASIC_CLASS = BasicTestExample.class;
private static final Class SIMPLE_CLASS = SimpleTestExample.class;


private static final Runnable FAILING_BLOCK = () -> fail("should fail");
private static final Runnable SUCCEEDING_TEST = () -> {
Expand All @@ -78,6 +83,29 @@ public class EnhancedLegacyXmlTest {
Path tempDirectory;
private static final String REPORT_NAME = "TEST-junit-jupiter.xml";

@Test
public void shouldReportPerTestClass() throws Exception {
String customProperties = "#report_filename=custom-report-junit\n# report_directory=reports\nreports_per_class=true\n";
Path customPropertiesFile = Files.createTempFile("xray-junit-extensions", ".properties");
Files.write(customPropertiesFile, customProperties.getBytes());

executeTestClasses(new Class[] { BASIC_CLASS, SIMPLE_CLASS }, customPropertiesFile, Clock.systemDefaultZone());

String reportName = "TEST-app.getxray.xray.junit.customjunitxml.BasicTestExample.xml";
Match testsuite = readValidXmlFile(tempDirectory.resolve(reportName));
// assert that suite childrens contains 2 testcase elements
assertThat(testsuite.children("testcase")).hasSize(2);
assertThat(testsuite.children("testcase").matchAttr("name", "someBasicTest")).isNotEmpty();
assertThat(testsuite.children("testcase").matchAttr("name", "anotherBasicTest")).isNotEmpty();


reportName = "TEST-app.getxray.xray.junit.customjunitxml.SimpleTestExample.xml";
testsuite = readValidXmlFile(tempDirectory.resolve(reportName));
assertThat(testsuite.children("testcase")).hasSize(2);
assertThat(testsuite.children("testcase").matchAttr("name", "someSimpleTest")).isNotEmpty();
assertThat(testsuite.children("testcase").matchAttr("name", "anotherSimpleTest")).isNotEmpty();
}


@Test
public void shouldSupportCustomReportNames() throws Exception {
Expand Down Expand Up @@ -421,6 +449,17 @@ private void executeTestMethodWithParams(Class<?> testClass, String methodName,
launcher.execute(discoveryRequest, listener);
}

private void executeTestClasses(Class<?>[] testClasses, Path propertiesPath,Clock clock) {
// select several classes to execute in junit 5
LauncherDiscoveryRequest discoveryRequest = request()
.selectors(Arrays.stream(testClasses).map(DiscoverySelectors::selectClass).collect(Collectors.toList()))
.build();
Launcher launcher = LauncherFactory.create();
EnhancedLegacyXmlReportGeneratingListener listener = new EnhancedLegacyXmlReportGeneratingListener(tempDirectory, propertiesPath, new PrintWriter(System.out), clock);
launcher.execute(discoveryRequest, listener);
}


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

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

public class SimpleTestExample {

@Test
@Order(1)
public void someSimpleTest() {
}

@Test
@Order(2)
public void anotherSimpleTest() {
}
}

3 changes: 2 additions & 1 deletion src/test/resources/xray-junit-extensions.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# report_filename=custom-report-junit
# report_directory=reports
# add_timestamp_to_report_filename=true
# add_timestamp_to_report_filename=true
# reports_per_class=true

0 comments on commit da1b57a

Please sign in to comment.