Skip to content

Commit

Permalink
add support for stdout in trx results (via #845)
Browse files Browse the repository at this point in the history
  • Loading branch information
P-Hessler authored and baev committed Oct 9, 2018
1 parent c2e11aa commit 31de5e2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import io.qameta.allure.context.RandomUidContext;
import io.qameta.allure.core.Configuration;
import io.qameta.allure.core.ResultsVisitor;
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 @@ -24,11 +26,13 @@
import java.time.ZonedDateTime;
import java.time.chrono.ChronoZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
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 static io.qameta.allure.entity.LabelName.RESULT_FORMAT;
import static java.nio.file.Files.newDirectoryStream;
Expand Down Expand Up @@ -65,6 +69,7 @@ public class TrxPlugin implements Reader {
public static final String MESSAGE_ELEMENT_NAME = "Message";
public static final String STACK_TRACE_ELEMENT_NAME = "StackTrace";
public static final String ERROR_INFO_ELEMENT_NAME = "ErrorInfo";
public static final String STDOUT_ELEMENT_NAME = "StdOut";

@Override
public void readResults(final Configuration configuration,
Expand Down Expand Up @@ -161,6 +166,16 @@ protected void parseUnitTestResult(final XmlElement unitTestResult,
.setTime(getTime(startTime, endTime));
getStatusMessage(unitTestResult).ifPresent(result::setStatusMessage);
getStatusTrace(unitTestResult).ifPresent(result::setStatusTrace);
getLogMessage(unitTestResult).ifPresent(logMessage -> {
final List<String> lines = splitLines(logMessage);
final List<Step> steps = lines
.stream()
.map(line -> new Step().setName(line))
.collect(Collectors.toList());
final StageResult stageResult = new StageResult()
.setSteps(steps);
result.setTestStage(stageResult);
});
Optional.ofNullable(tests.get(executionId)).ifPresent(unitTest -> {
result.setParameters(unitTest.getParameters());
result.setDescription(unitTest.getDescription());
Expand All @@ -170,6 +185,16 @@ protected void parseUnitTestResult(final XmlElement unitTestResult,
visitor.visitTestResult(result);
}

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

private Optional<String> getLogMessage(final XmlElement unitTestResult) {
return unitTestResult.getFirst(OUTPUT_ELEMENT_NAME)
.flatMap(output -> output.getFirst(STDOUT_ELEMENT_NAME))
.map(XmlElement::getValue);
}

private Optional<String> getStatusMessage(final XmlElement unitTestResult) {
return unitTestResult.getFirst(OUTPUT_ELEMENT_NAME)
.flatMap(output -> output.getFirst(ERROR_INFO_ELEMENT_NAME))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ public void shouldParseErrorInfo() throws Exception {
.containsExactly(tuple("Some message", "Some trace"));
}

@Test
public void shouldParseStdOutOnFail() throws Exception {
process(
"trxdata/sample.trx",
"sample.trx"
);

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

assertThat(captor.getAllValues())
.filteredOn(result -> result.getStatus() == Status.FAILED)
.filteredOn(result -> result.getTestStage().getSteps().size() == 10)
.filteredOn(result -> result.getTestStage().getSteps().get(1).getName().contains("Given I have entered 50 into the calculator"))
.filteredOn(result -> result.getTestStage().getSteps().get(3).getName().contains("And I have entered -1 into the calculator"))
.hasSize(1);
}

private void process(String... strings) throws IOException {
Path resultsDirectory = folder.newFolder().toPath();
Iterator<String> iterator = Arrays.asList(strings).iterator();
Expand Down

0 comments on commit 31de5e2

Please sign in to comment.