Skip to content

Commit

Permalink
Add JUnit attachment support
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisnikoli committed Aug 28, 2024
1 parent 82ea6b2 commit 14101fc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.qameta.allure.datetime.DateTimeParser;
import io.qameta.allure.datetime.LocalDateTimeParser;
import io.qameta.allure.datetime.ZonedDateTimeParser;
import io.qameta.allure.entity.Attachment;
import io.qameta.allure.entity.LabelName;
import io.qameta.allure.entity.Parameter;
import io.qameta.allure.entity.StageResult;
Expand All @@ -44,6 +45,7 @@
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
Expand All @@ -54,12 +56,13 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.qameta.allure.entity.LabelName.RESULT_FORMAT;
import static java.nio.file.Files.newDirectoryStream;
import static java.util.Collections.singletonList;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

Expand Down Expand Up @@ -100,6 +103,8 @@ public class JunitXmlPlugin implements Reader {

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

private static final Pattern SYSTEM_OUTPUT_ATTACHMENT = Pattern.compile(" *\\[\\[ATTACHMENT\\|(?<path>.*)\\]\\]");

private static final Map<String, Status> RETRIES;

static {
Expand Down Expand Up @@ -185,19 +190,34 @@ private void parseTestCase(final TestSuiteInfo info, final XmlElement testCaseEl
result.setFlaky(isFlaky(testCaseElement));
setStatusDetails(result, testCaseElement);
final StageResult stageResult = new StageResult();
final List<Attachment> attachments = new ArrayList<>();
getLogMessage(testCaseElement).ifPresent(logMessage -> {
final List<String> lines = splitLines(logMessage);
final List<Step> steps = lines
.stream()
.filter(line -> !SYSTEM_OUTPUT_ATTACHMENT.matcher(line).matches())
.map(line -> new Step().setName(line))
.collect(Collectors.toList());
stageResult.setSteps(steps);
lines
.stream()
.filter(line -> SYSTEM_OUTPUT_ATTACHMENT.matcher(line).matches())
.forEach(line -> {
final Matcher m = SYSTEM_OUTPUT_ATTACHMENT.matcher(line);
if (m.find()) {
final Path attachmentPath = Paths.get(m.group("path")).toAbsolutePath();
final Attachment attachment = visitor.visitAttachmentFile(attachmentPath);
attachment.setName(attachmentPath.getFileName().toString());
attachments.add(attachment);
}
});
});
getLogFile(resultsDirectory, className)
.filter(Files::exists)
.map(visitor::visitAttachmentFile)
.map(attachment1 -> attachment1.setName("System out"))
.ifPresent(attachment -> stageResult.setAttachments(singletonList(attachment)));
.ifPresent(attachments::add);
stageResult.setAttachments(attachments);
result.setTestStage(stageResult);
visitor.visitTestResult(result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,42 @@ void shouldAddLogAsAttachment() throws Exception {
assertThat(testStage.getAttachments())
.describedAs("Should add an attachment")
.hasSize(1)
.describedAs("Attachment should has right uid and name")
.describedAs("Attachment should have right uid and name")
.extracting(Attachment::getName, Attachment::getUid)
.containsExactly(Tuple.tuple("System out", "some-uid"));
}

@Test
void shouldAddSystemOutAsAttachment() throws Exception {
final Attachment hey = new Attachment().setUid("some-uid");
when(visitor.visitAttachmentFile(any())).thenReturn(hey);
process(
"junitdata/TEST-test.Attachment.xml", "TEST-test.Attachment.xml"
);

final ArgumentCaptor<Path> attachmentCaptor = ArgumentCaptor.captor();
verify(visitor, times(1)).visitAttachmentFile(attachmentCaptor.capture());

assertThat(attachmentCaptor.getValue())
.isRegularFile()
.hasContent("some-test-log");

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

final StageResult testStage = captor.getValue().getTestStage();
assertThat(testStage)
.describedAs("Should create a test stage")
.isNotNull();

assertThat(testStage.getAttachments())
.describedAs("Should add an attachment")
.hasSize(1)
.describedAs("Attachment should have right uid and name")
.extracting(Attachment::getName, Attachment::getUid)
.containsExactly(Tuple.tuple("test.SampleTest.txt", "some-uid"));
}

@Test
void shouldAddLabels() throws Exception {
process(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite tests="1" failures="1" name="test.Attachment" time="2.055" errors="0" skipped="0">
<testcase classname="test.Attachment" name="shouldAddAttachment" time="1.051">
<system-out>Step 1
Step 2
Step 3
[[ATTACHMENT|src/test/resources/junitdata/test.SampleTest.txt]]</system-out>
<failure message="some-message"><![CDATA[some-trace]]></failure>
</testcase>
</testsuite>

0 comments on commit 14101fc

Please sign in to comment.