Skip to content

Commit

Permalink
Background steps handling
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Dec 21, 2023
1 parent e5bfcce commit 74d3147
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import static org.apache.commons.lang3.StringUtils.isNotBlank;

public class ReportPortalPublisher {
public static final String BACKGROUND_PREFIX = "BACKGROUND: ";
public static final String SCENARIO_CODE_REFERENCE_PATTERN = "%s/[SCENARIO:%s]";
public static final String EXAMPLE_CODE_REFERENCE_PATTERN = "%s/[EXAMPLE:%s%s]";
public static final String VARIABLE_PATTERN =
Expand Down Expand Up @@ -349,6 +350,9 @@ public void finishScenario(ScenarioResult scenarioResult) {
protected StartTestItemRQ buildStartStepRq(@Nonnull StepResult stepResult, @Nonnull ScenarioResult scenarioResult) {
Step step = stepResult.getStep();
String stepName = step.getPrefix() + " " + step.getText();
if (step.isBackground()) {
stepName = BACKGROUND_PREFIX + stepName;
}
StartTestItemRQ rq = buildStartTestItemRq(stepName, getStepStartTime(stepStartTimeMap, stepId), ItemType.STEP);
rq.setHasStats(false);
if (step.isOutline()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.epam.reportportal.karate.background;

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.ta.reportportal.ws.model.StartTestItemRQ;
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.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.*;

public class BackgroundTest {
private final String featureId = CommonUtils.namedId("feature_");
private final String scenarioId = CommonUtils.namedId("scenario_");
private final List<String> stepIds = Stream.generate(() -> CommonUtils.namedId("step_"))
.limit(3).collect(Collectors.toList());


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

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

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

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client).startTestItem(captor.capture());
verify(client, times(1)).startTestItem(same(featureId), captor.capture());
ArgumentCaptor<StartTestItemRQ> stepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(3)).startTestItem(same(scenarioId), stepCaptor.capture());

List<StartTestItemRQ> items = captor.getAllValues();
assertThat(items, hasSize(2));
List<StartTestItemRQ> steps = stepCaptor.getAllValues();
assertThat(steps, hasSize(3));

assertThat(steps.get(0).getName(), equalTo("BACKGROUND: Given def four = 4"));
}
}
8 changes: 8 additions & 0 deletions src/test/resources/feature/background.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Feature: the test to show item description reporting

Background: Set variable
Given def four = 4

Scenario: Verify math
When def acualFour = 2 * 2
Then assert acualFour == four

0 comments on commit 74d3147

Please sign in to comment.