Skip to content

Commit

Permalink
adds JUnit's system-out to report (via #990)
Browse files Browse the repository at this point in the history
  • Loading branch information
aptester authored and Artem Eroshenko committed Sep 8, 2019
1 parent 7094906 commit d74e7c6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.qameta.allure.entity.LabelName;
import io.qameta.allure.entity.StageResult;
import io.qameta.allure.entity.Status;
import io.qameta.allure.entity.Step;
import io.qameta.allure.entity.TestResult;
import io.qameta.allure.entity.Time;
import io.qameta.allure.parser.XmlElement;
Expand All @@ -44,13 +45,15 @@
import java.nio.file.Path;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.qameta.allure.entity.LabelName.RESULT_FORMAT;
Expand Down Expand Up @@ -89,6 +92,7 @@ public class JunitXmlPlugin implements Reader {
private static final String TIMESTAMP_ATTRIBUTE_NAME = "timestamp";
private static final String STATUS_ATTRIBUTE_NAME = "status";
private static final String SKIPPED_ATTRIBUTE_VALUE = "notrun";
private static final String SYSTEM_OUTPUT_ELEMENT_NAME = "system-out";

private static final String XML_GLOB = "*.xml";

Expand Down Expand Up @@ -157,7 +161,7 @@ private void parseTestSuite(final XmlElement testSuiteElement, final Path parsed
.setTimestamp(getUnix(timestamp));
testSuiteElement.get(TEST_CASE_ELEMENT_NAME)
.forEach(element -> parseTestCase(info, element,
resultsDirectory, parsedFile, context, visitor));
resultsDirectory, parsedFile, context, visitor));
}

private Long getUnix(final String timestamp) {
Expand All @@ -176,15 +180,21 @@ private void parseTestCase(final TestSuiteInfo info, final XmlElement testCaseEl
result.setStatus(status);
result.setFlaky(isFlaky(testCaseElement));
setStatusDetails(result, testCaseElement);

final StageResult stageResult = new StageResult();
getLogMessage(testCaseElement).ifPresent(logMessage -> {
final List<String> lines = splitLines(logMessage);
final List<Step> steps = lines
.stream()
.map(line -> new Step().setName(line))
.collect(Collectors.toList());
stageResult.setSteps(steps);
});
getLogFile(resultsDirectory, className)
.filter(Files::exists)
.map(visitor::visitAttachmentFile)
.map(attachment1 -> attachment1.setName("System out"))
.ifPresent(attachment -> result.setTestStage(
new StageResult().setAttachments(singletonList(attachment))
));

.ifPresent(attachment -> stageResult.setAttachments(singletonList(attachment)));
result.setTestStage(stageResult);
visitor.visitTestResult(result);

RETRIES.forEach((elementName, retryStatus) -> testCaseElement.get(elementName).forEach(failure -> {
Expand All @@ -197,6 +207,14 @@ private void parseTestCase(final TestSuiteInfo info, final XmlElement testCaseEl
}));
}

private List<String> splitLines(final String str) {
return Arrays.asList(str.split("\\r?\\n"));
}

private Optional<String> getLogMessage(final XmlElement testCaseElement) {
return testCaseElement.getFirst(SYSTEM_OUTPUT_ELEMENT_NAME).map(XmlElement::getValue);
}

private Optional<Path> getLogFile(final Path resultsDirectory, final String className) {
try {
return Optional.ofNullable(className)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,36 @@ void shouldProcessTestsWithRetry() throws Exception {
}

@Test
void shouldReadCdataMessage() throws Exception {
void shouldReadStatusMessage() throws Exception {
process(
"junitdata/TEST-test.CdataMessage.xml", "TEST-test.SampleTest.xml"
);


final ArgumentCaptor<TestResult> captor = ArgumentCaptor.forClass(TestResult.class);
verify(visitor, times(1)).visitTestResult(captor.capture());
verify(visitor, times(2)).visitTestResult(captor.capture());

assertThat(captor.getAllValues())
.extracting(TestResult::getStatusMessage, TestResult::getStatusTrace)
.containsExactlyInAnyOrder(
tuple("some-message", "some-trace")
tuple("some-message", "some-trace"),
tuple(null,null)
);
}

@Test
void shouldReadSystemOutMessage() throws Exception {
process(
"junitdata/TEST-test.CdataMessage.xml", "TEST-test.SampleTest.xml"
);

final ArgumentCaptor<TestResult> captor = ArgumentCaptor.forClass(TestResult.class);
verify(visitor, times(2)).visitTestResult(captor.capture());

assertThat(captor.getAllValues())
.filteredOn(result -> result.getTestStage().getSteps().size() == 2)
.filteredOn(result -> result.getTestStage().getSteps().get(0).getName().equals("output"))
.filteredOn(result -> result.getTestStage().getSteps().get(1).getName().equals("more output"));
}

@Issue("532")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite tests="1" failures="0" name="test.SampleTest" time="1.051" errors="0" skipped="0">
<testcase classname="test.SampleTest" name="shouldGenerate" time="1.051">
<failure message="some-message"><![CDATA[some-trace]]></failure>
</testcase>
<testsuite tests="2" failures="0" name="test.SampleTest" time="2.055" errors="0" skipped="0">
<testcase classname="test.SampleTest" name="shouldGenerateSystemOutOnError" time="1.051">
<system-out><![CDATA[output
more output]]></system-out>
<failure message="some-message"><![CDATA[some-trace]]></failure>
</testcase>
<testcase classname="test.SampleTest" name="shouldGenerateSystemOut" time="1.004">
<system-out><![CDATA[test output]]></system-out>
</testcase>
</testsuite>

0 comments on commit d74e7c6

Please sign in to comment.