Skip to content

Commit

Permalink
Add parameter description to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Dec 26, 2023
1 parent e5bdda6 commit e8b2c7f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.epam.reportportal.service.item.TestCaseIdEntry;
import com.epam.reportportal.utils.AttributeParser;
import com.epam.reportportal.utils.MemoizingSupplier;
import com.epam.reportportal.utils.ParameterUtils;
import com.epam.reportportal.utils.TestCaseIdUtils;
import com.epam.reportportal.utils.properties.SystemAttributesExtractor;
import com.epam.ta.reportportal.ws.model.FinishExecutionRQ;
Expand Down Expand Up @@ -60,6 +61,7 @@ public class ReportPortalPublisher {
public static final String VARIABLE_PATTERN =
"(?:(?<=#\\()%1$s(?=\\)))|(?:(?<=[\\s=+-/*<>(]|^)%1$s(?=[\\s=+-/*<>)]|(?:\\r?\\n)|$))";
public static final String MARKDOWN_DELIMITER_PATTERN = "%s\n\n---\n\n%s";
public static final String PARAMETERS_PATTERN = "Parameters:\n\n%s";

private static final Logger LOGGER = LoggerFactory.getLogger(ReportPortalPublisher.class);
private final Map<String, Maybe<String>> featureIdMap = new HashMap<>();
Expand Down Expand Up @@ -310,10 +312,24 @@ protected StartTestItemRQ buildStartScenarioRq(@Nonnull ScenarioResult scenarioR
rq.setCodeRef(getCodeRef(scenario));
rq.setTestCaseId(ofNullable(getTestCaseId(scenario)).map(TestCaseIdEntry::getId).orElse(null));
rq.setAttributes(toAttributes(scenario.getTags()));
rq.setParameters(getParameters(scenario));
List<ParameterResource> parameters = getParameters(scenario);
boolean hasParameters = ofNullable(parameters).filter(p -> !p.isEmpty()).isPresent();
if (hasParameters) {
rq.setParameters(parameters);
}

String description = scenario.getDescription();
if (isNotBlank(description)) {
rq.setDescription(description);
if (hasParameters) {
rq.setDescription(
String.format(MARKDOWN_DELIMITER_PATTERN,
String.format(PARAMETERS_PATTERN, ParameterUtils.formatParametersAsTable(parameters)),
description));
} else {
rq.setDescription(description);
}
} else if (hasParameters) {
rq.setDescription(String.format(PARAMETERS_PATTERN, ParameterUtils.formatParametersAsTable(parameters)));
}
return rq;
}
Expand Down Expand Up @@ -376,7 +392,8 @@ protected StartTestItemRQ buildStartStepRq(@Nonnull StepResult stepResult, @Nonn
.getExampleData()
.entrySet()
.stream()
.filter(e -> Pattern.compile(String.format(VARIABLE_PATTERN, e.getKey())).matcher(step.getText()).find())
.filter(e -> Pattern.compile(String.format(VARIABLE_PATTERN, e.getKey()))
.matcher(step.getText()).find())
.map(e -> {
ParameterResource param = new ParameterResource();
param.setKey(e.getKey());
Expand All @@ -400,7 +417,8 @@ public void startStep(StepResult stepResult, ScenarioResult scenarioResult) {
if (stepResult.getStep().isBackground()) {
backgroundId = ofNullable(backgroundId).orElseGet(() -> {
StartTestItemRQ backgroundRq = buildStartBackgroundRq(stepResult, scenarioResult);
return launch.get().startTestItem(scenarioIdMap.get(scenarioResult.getScenario().getName()), backgroundRq);
return launch.get().startTestItem(scenarioIdMap.get(scenarioResult.getScenario().getName()),
backgroundRq);
});
} else {
backgroundId = null;
Expand All @@ -415,7 +433,8 @@ public void startStep(StepResult stepResult, ScenarioResult scenarioResult) {
ofNullable(stepRq.getParameters())
.filter(params -> !params.isEmpty())
.ifPresent(params ->
sendLog(stepId, "Parameters:\n\n" + formatParametersAsTable(params), LogLevel.INFO));
sendLog(stepId, String.format(PARAMETERS_PATTERN, formatParametersAsTable(params)),
LogLevel.INFO));
ofNullable(stepResult.getStep().getTable())
.ifPresent(table ->
sendLog(stepId, "Table:\n\n" + formatDataTable(table.getRows()), LogLevel.INFO));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.epam.reportportal.karate.description;

import com.epam.reportportal.karate.utils.TestUtils;
import com.epam.reportportal.service.ReportPortal;
import com.epam.reportportal.service.ReportPortalClient;
import com.epam.reportportal.util.test.CommonUtils;
import com.epam.reportportal.utils.markdown.MarkdownUtils;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.epam.reportportal.karate.utils.TestUtils.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.*;

public class NoDescriptionExamplesTest {
private final String featureId = CommonUtils.namedId("feature_");
private final List<String> exampleIds = Stream.generate(() -> CommonUtils.namedId("example_")).limit(2)
.collect(Collectors.toList());
private final List<Pair<String, List<String>>> stepIds = exampleIds.stream()
.map(e -> Pair.of(e, Stream.generate(() -> CommonUtils.namedId("step_"))
.limit(2).collect(Collectors.toList())))
.collect(Collectors.toList());

private static final String EXAMPLE_PARAMETERS_DESCRIPTION_PATTERN =
"Parameters:\n\n"
+ MarkdownUtils.TABLE_INDENT
+ "| vara | varb | result |\n"
+ MarkdownUtils.TABLE_INDENT
+ "|------|------|--------|\n"
+ MarkdownUtils.TABLE_INDENT;
private static final String FIRST_EXAMPLE_DESCRIPTION = EXAMPLE_PARAMETERS_DESCRIPTION_PATTERN
+ "|  2   |  2   |   4    |";
private static final String SECOND_EXAMPLE_DESCRIPTION = EXAMPLE_PARAMETERS_DESCRIPTION_PATTERN
+ "|  1   |  2   |   3    |";

private final ReportPortalClient client = mock(ReportPortalClient.class);
private final ReportPortal rp = ReportPortal.create(client, standardParameters(), testExecutor());

@BeforeEach
public void setupMock() {
mockLaunch(client, null, featureId, stepIds);
mockBatchLogging(client);
}

@Test
public void test_examples_no_description() {
var results = TestUtils.runAsReport(rp, "classpath:feature/examples.feature");
assertThat(results.getFailCount(), equalTo(0));

ArgumentCaptor<StartTestItemRQ> featureCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(1)).startTestItem(featureCaptor.capture());
ArgumentCaptor<StartTestItemRQ> scenarioCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(2)).startTestItem(same(featureId), scenarioCaptor.capture());
ArgumentCaptor<StartTestItemRQ> firstStepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(2)).startTestItem(same(exampleIds.get(0)), firstStepCaptor.capture());
ArgumentCaptor<StartTestItemRQ> secondStepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(2)).startTestItem(same(exampleIds.get(1)), secondStepCaptor.capture());

List<StartTestItemRQ> scenarios = scenarioCaptor.getAllValues();
StartTestItemRQ firstScenarioRq = scenarios.get(0);
assertThat(firstScenarioRq.getDescription(), allOf(notNullValue(), equalTo(FIRST_EXAMPLE_DESCRIPTION)));

StartTestItemRQ secondScenarioRq = scenarios.get(1);
assertThat(secondScenarioRq.getDescription(), allOf(notNullValue(), equalTo(SECOND_EXAMPLE_DESCRIPTION)));
}
}

0 comments on commit e8b2c7f

Please sign in to comment.