Skip to content

Commit

Permalink
Merge pull request #36 from Xray-App/report_only_annotated_tests
Browse files Browse the repository at this point in the history
preliminary implementation to report only annotated tests. fixes #24
  • Loading branch information
bitcoder authored Dec 4, 2023
2 parents c3361c3 + ead2431 commit bc93da4
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 23 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Add the following dependency to your pom.xml:
<dependency>
<groupId>app.getxray</groupId>
<artifactId>xray-junit-extensions</artifactId>
<version>0.6.2</version>
<version>0.7.0</version>
<scope>test</scope>
</dependency>
```
Expand All @@ -60,14 +60,17 @@ If you want, you may configure certain aspects of this extension. The defaults s

- `report_filename`: the name of the report, without ending .xml suffix. Default is "TEST-junit-jupiter.xml"
- `report_directory`: the directory where to generate the report, in relative or absolute format. Default is "target"
- `add_timestamp_to_report_filename`: the name of the report, without ending .xml suffix. Default is "false".
- `add_timestamp_to_report_filename`: add a timestamp based suffix to the report. Default is "false".
- `report_only_annotated_tests`: only include tests annotated with @XrayTest or @Requirement. Default is "false".


Example:

```bash
report_filename=custom-report-junit
report_directory=reports
add_timestamp_to_report_filename=true
report_only_annotated_tests=false
```

## How to use
Expand Down
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.6.2</version>
<version>0.7.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 @@ -21,6 +21,8 @@

import javax.xml.stream.XMLStreamException;

import static org.junit.jupiter.api.DynamicTest.stream;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -61,6 +63,7 @@ public class EnhancedLegacyXmlReportGeneratingListener implements TestExecutionL

private String reportFilename = null;
boolean addTimestampToReportFilename = false;
boolean reportOnlyAnnotatedTests = false;

private XmlReportData reportData;

Expand All @@ -75,16 +78,7 @@ public EnhancedLegacyXmlReportGeneratingListener(Path reportsDir, Path propertie
this.out = out;
this.clock = clock;

try {

InputStream stream = null;
if (propertiesFile == null) {
stream = getClass().getClassLoader().getResourceAsStream("xray-junit-extensions.properties");
} else {
// For tests only
stream = Files.newInputStream(propertiesFile);
}

try (InputStream stream = (propertiesFile == null) ? getClass().getClassLoader().getResourceAsStream("xray-junit-extensions.properties") : Files.newInputStream(propertiesFile)) {
// if properties exist, or are enforced from the test, then process them
if (stream != null) {
Properties properties = new Properties();
Expand All @@ -98,6 +92,7 @@ public EnhancedLegacyXmlReportGeneratingListener(Path reportsDir, Path propertie
this.reportsDir = FileSystems.getDefault().getPath(customReportsDirectory);
}
this.addTimestampToReportFilename = "true".equals(properties.getProperty("add_timestamp_to_report_filename"));
this.reportOnlyAnnotatedTests = "true".equals(properties.getProperty("report_only_annotated_tests", "false"));
} else {
if (reportsDir == null) {
this.reportsDir = FileSystems.getDefault().getPath(DEFAULT_REPORTS_DIR);
Expand Down Expand Up @@ -181,7 +176,7 @@ private void writeXmlReportSafely(TestIdentifier testIdentifier, String rootName
xmlFile = this.reportsDir.resolve(fileName);

try (Writer fileWriter = Files.newBufferedWriter(xmlFile)) {
new XmlReportWriter(this.reportData).writeXmlReport(testIdentifier, fileWriter);
new XmlReportWriter(this.reportData, this.reportOnlyAnnotatedTests).writeXmlReport(testIdentifier, fileWriter);
} catch (XMLStreamException | IOException e) {
printException("Could not write XML report: " + xmlFile, e);
logger.error(e, () -> "Could not write XML report: " + xmlFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ class XmlReportWriter {

private final XmlReportData reportData;
private static final Logger logger = LoggerFactory.getLogger(EnhancedLegacyXmlReportGeneratingListener.class);
private boolean reportOnlyAnnotatedTests = false;

XmlReportWriter(XmlReportData reportData) {
XmlReportWriter(XmlReportData reportData, boolean reportOnlyAnnotatedTests) {
this.reportData = reportData;
this.reportOnlyAnnotatedTests = reportOnlyAnnotatedTests;
}

void writeXmlReport(TestIdentifier rootDescriptor, Writer out) throws XMLStreamException {
Expand Down Expand Up @@ -229,6 +231,14 @@ private Map<String, String> getTestRunCustomFields(List<ReportEntry> entries) {
private void writeTestcase(TestIdentifier testIdentifier, AggregatedTestResult testResult,
NumberFormat numberFormat, XMLStreamWriter writer) throws XMLStreamException {

final Optional<TestSource> testSource = testIdentifier.getSource();
final Optional<Method> testMethod = testSource.flatMap(this::getTestMethod);
Optional<XrayTest> xrayTest = AnnotationSupport.findAnnotation(testMethod, XrayTest.class);
Optional<Requirement> requirement = AnnotationSupport.findAnnotation(testMethod, Requirement.class);
if (reportOnlyAnnotatedTests && (!requirement.isPresent() && !xrayTest.isPresent())) {
return;
}

writer.writeStartElement("testcase");
writeAttributeSafely(writer, "name", getName(testIdentifier));
writeAttributeSafely(writer, "classname", getClassName(testIdentifier));
Expand Down Expand Up @@ -260,17 +270,13 @@ private void writeTestcase(TestIdentifier testIdentifier, AggregatedTestResult t
newLine(writer);
}

final Optional<TestSource> testSource = testIdentifier.getSource();
final Optional<Method> testMethod = testSource.flatMap(this::getTestMethod);
// final Optional<Class<?>> testClass = testSource.flatMap(this::getTestClass);

Optional<Requirement> requirement = AnnotationSupport.findAnnotation(testMethod, Requirement.class);
if (requirement.isPresent()) {
String[] requirements = requirement.get().value();
addProperty(writer, "requirements", String.join(",", requirements));
}

Optional<XrayTest> xrayTest = AnnotationSupport.findAnnotation(testMethod, XrayTest.class);
String test_key = null;
String test_id = null;
String test_summary = null;
Expand Down Expand Up @@ -319,10 +325,6 @@ private void writeTestcase(TestIdentifier testIdentifier, AggregatedTestResult t

List<ReportEntry> entries = this.reportData.getReportEntries(testIdentifier);
Map<String, String> testrunCustomFields = getTestRunCustomFields(entries);
//for (Map.Entry<String, String> customField : testrunCustomFields.entrySet()) {
// addProperty(writer, customField.getKey(), customField.getValue());
//}


if (!testrunCustomFields.isEmpty()) {
writer.writeStartElement("property");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,76 @@ public void shouldSupportCustomReportRelativeDirectory() throws Exception {
assertThat(testcase.child("properties").children("property").matchAttr("name", "requirements")).isEmpty();
}

@Test
public void withReportAnnotatedTestsReportAnnotatedXrayTest() throws Exception {
String testMethodName = "annotatedXrayTestWithKey";
String customProperties = "#report_filename=custom-report-junit\n#report_directory=\n# add_timestamp_to_report_filename=true\nreport_only_annotated_tests=true\n";
Path customPropertiesFile = Files.createTempFile("xray-junit-extensions", ".properties");
Files.write(customPropertiesFile, customProperties.getBytes());
executeTestMethodWithCustomProperties(TEST_EXAMPLES_CLASS, testMethodName, customPropertiesFile, "", Clock.systemDefaultZone());

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").attr("value")).isEqualTo("CALC-100");
assertThat(testcase.child("properties").children("property").matchAttr("name", "test_summary")).isEmpty();
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 withReportAnnotatedTestsReportAnnotatedRequirement() throws Exception {
String testMethodName = "annotatedWithOneRequirement";
String customProperties = "#report_filename=custom-report-junit\n#report_directory=\n# add_timestamp_to_report_filename=true\nreport_only_annotated_tests=true\n";
Path customPropertiesFile = Files.createTempFile("xray-junit-extensions", ".properties");
Files.write(customPropertiesFile, customProperties.getBytes());
executeTestMethodWithCustomProperties(TEST_EXAMPLES_CLASS, testMethodName, customPropertiesFile, "", Clock.systemDefaultZone());

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")).isEmpty();
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").attr("value")).isEqualTo("CALC-123");
}

@Test
public void withReportAnnotatedTestsDontReportNonAnnotated() throws Exception {
String testMethodName = "legacyTest";
String customProperties = "#report_filename=custom-report-junit\n#report_directory=\n# add_timestamp_to_report_filename=true\nreport_only_annotated_tests=true\n";
Path customPropertiesFile = Files.createTempFile("xray-junit-extensions", ".properties");
Files.write(customPropertiesFile, customProperties.getBytes());
executeTestMethodWithCustomProperties(TEST_EXAMPLES_CLASS, testMethodName, customPropertiesFile, "", Clock.systemDefaultZone());

Match testsuite = readValidXmlFile(tempDirectory.resolve(REPORT_NAME));
Match testcase = testsuite.child("testcase");
assertThat(testcase).isNullOrEmpty();
}

@Test
public void withoutReportAnnotatedTestsReportAllTests() throws Exception {
String testMethodName = "legacyTest";
String customProperties = "#report_filename=custom-report-junit\n#report_directory=\n# add_timestamp_to_report_filename=true\n"+"report_only_annotated_tests=false\n";
Path customPropertiesFile = Files.createTempFile("xray-junit-extensions", ".properties");
Files.write(customPropertiesFile, customProperties.getBytes());
executeTestMethodWithCustomProperties(TEST_EXAMPLES_CLASS, testMethodName, customPropertiesFile, "", Clock.systemDefaultZone());

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")).isEmpty();
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 simpleTestShouldNotHaveXrayProperties() throws Exception {
String testMethodName = "legacyTest";
Expand Down

0 comments on commit bc93da4

Please sign in to comment.