Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trx logger parses stdout on fail #845

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,18 @@ protected void parseUnitTestResult(final XmlElement unitTestResult,
.setTime(getTime(startTime, endTime));
getStatusMessage(unitTestResult).ifPresent(result::setStatusMessage);
getStatusTrace(unitTestResult).ifPresent(result::setStatusTrace);
if (result.getStatus() != Status.PASSED) {
P-Hessler marked this conversation as resolved.
Show resolved Hide resolved
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 +187,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