From da1b57a20aac6f38b82d367ae90c53356155274b Mon Sep 17 00:00:00 2001 From: Sergio Freire Date: Tue, 2 Apr 2024 12:30:23 +0100 Subject: [PATCH] add test for reports per test class and fixed logic for name of report --- pom.xml | 2 +- ...ncedLegacyXmlReportGeneratingListener.java | 16 ++++++-- .../customjunitxml/BasicTestExample.java | 18 ++++++++ .../customjunitxml/EnhancedLegacyXmlTest.java | 41 ++++++++++++++++++- .../customjunitxml/SimpleTestExample.java | 18 ++++++++ .../xray-junit-extensions.properties | 3 +- 6 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 src/test/java/app/getxray/xray/junit/customjunitxml/BasicTestExample.java create mode 100644 src/test/java/app/getxray/xray/junit/customjunitxml/SimpleTestExample.java diff --git a/pom.xml b/pom.xml index 95c959c..f5eb6fb 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ app.getxray xray-junit-extensions jar - 0.7.3 + 0.8.0 xray-junit-extensions Improvements for JUnit that allow you to take better advantage of JUnit 5 (jupiter engine) whenever using it together with Xray Test Management. https://github.com/Xray-App/xray-junit-extensions diff --git a/src/main/java/app/getxray/xray/junit/customjunitxml/EnhancedLegacyXmlReportGeneratingListener.java b/src/main/java/app/getxray/xray/junit/customjunitxml/EnhancedLegacyXmlReportGeneratingListener.java index 130f7df..9376d73 100644 --- a/src/main/java/app/getxray/xray/junit/customjunitxml/EnhancedLegacyXmlReportGeneratingListener.java +++ b/src/main/java/app/getxray/xray/junit/customjunitxml/EnhancedLegacyXmlReportGeneratingListener.java @@ -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; @@ -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); @@ -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); /* @@ -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) { diff --git a/src/test/java/app/getxray/xray/junit/customjunitxml/BasicTestExample.java b/src/test/java/app/getxray/xray/junit/customjunitxml/BasicTestExample.java new file mode 100644 index 0000000..a3f4ea0 --- /dev/null +++ b/src/test/java/app/getxray/xray/junit/customjunitxml/BasicTestExample.java @@ -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() { + } +} + diff --git a/src/test/java/app/getxray/xray/junit/customjunitxml/EnhancedLegacyXmlTest.java b/src/test/java/app/getxray/xray/junit/customjunitxml/EnhancedLegacyXmlTest.java index 17d550b..7667073 100644 --- a/src/test/java/app/getxray/xray/junit/customjunitxml/EnhancedLegacyXmlTest.java +++ b/src/test/java/app/getxray/xray/junit/customjunitxml/EnhancedLegacyXmlTest.java @@ -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; @@ -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; @@ -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; @@ -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 = () -> { @@ -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 { @@ -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"; diff --git a/src/test/java/app/getxray/xray/junit/customjunitxml/SimpleTestExample.java b/src/test/java/app/getxray/xray/junit/customjunitxml/SimpleTestExample.java new file mode 100644 index 0000000..d4c32ff --- /dev/null +++ b/src/test/java/app/getxray/xray/junit/customjunitxml/SimpleTestExample.java @@ -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() { + } +} + diff --git a/src/test/resources/xray-junit-extensions.properties b/src/test/resources/xray-junit-extensions.properties index cdcb469..cbc6043 100644 --- a/src/test/resources/xray-junit-extensions.properties +++ b/src/test/resources/xray-junit-extensions.properties @@ -1,3 +1,4 @@ # report_filename=custom-report-junit # report_directory=reports -# add_timestamp_to_report_filename=true \ No newline at end of file +# add_timestamp_to_report_filename=true +# reports_per_class=true \ No newline at end of file