Skip to content

Commit

Permalink
Another background test
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Dec 21, 2023
1 parent a7c5bda commit b5e86bb
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ public void finishFeature(FeatureResult featureResult) {
featureResult.isFailed() ? ItemStatus.FAILED : ItemStatus.PASSED);
//noinspection ReactiveStreamsUnusedPublisher
launch.get().finishTestItem(featureIdMap.remove(featureResult.getCallNameForReport()), rq);
backgroundId = null;
}

/**
Expand Down Expand Up @@ -338,6 +337,7 @@ public void finishScenario(ScenarioResult scenarioResult) {
Maybe<String> removedScenarioId = scenarioIdMap.remove(scenarioResult.getScenario().getName());
//noinspection ReactiveStreamsUnusedPublisher
launch.get().finishTestItem(removedScenarioId, rq);
backgroundId = null;
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ public void test_background_steps() {
assertThat(nestedSteps, hasSize(1));
StartTestItemRQ nestedStep = nestedSteps.get(0);
assertThat(nestedStep.getName(), equalTo("Given def four = 4"));
assertThat(nestedStep.isHasStats(), equalTo(Boolean.FALSE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.epam.reportportal.karate.background;

import com.epam.reportportal.karate.utils.TestUtils;
import com.epam.reportportal.listeners.ItemType;
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 com.intuit.karate.core.Background;
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.Set;
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.ArgumentMatchers.startsWith;
import static org.mockito.Mockito.*;

public class BackgroundTwoStepsTest {
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(2).collect(Collectors.toList());
private final List<Pair<String, String>> nestedStepIds = Stream.concat(
stepIds.stream().map(id -> Pair.of(id, CommonUtils.namedId("nested_step_"))),
stepIds.stream().map(id -> Pair.of(id, CommonUtils.namedId("nested_step_")))).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);
mockNestedSteps(client, nestedStepIds);
mockBatchLogging(client);
}

@Test
public void test_background_steps() {
var results = TestUtils.runAsReport(rp, "classpath:feature/background_two_steps.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(2)).startTestItem(same(scenarioId), stepCaptor.capture());
ArgumentCaptor<StartTestItemRQ> nestedStepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(2)).startTestItem(startsWith("step_"), nestedStepCaptor.capture());

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

List<StartTestItemRQ> backgroundSteps = steps.stream()
.filter(s -> s.getName().startsWith(Background.KEYWORD)).collect(Collectors.toList());
assertThat(backgroundSteps, hasSize(1));
StartTestItemRQ backgroundStep = backgroundSteps.get(0);
assertThat(backgroundStep.getName(), equalTo(Background.KEYWORD)); // No name for Background in Karate
assertThat(backgroundStep.isHasStats(), equalTo(Boolean.FALSE));
assertThat(backgroundStep.getStartTime(), notNullValue());
assertThat(backgroundStep.getType(), equalTo(ItemType.STEP.name()));

List<StartTestItemRQ> nestedSteps = nestedStepCaptor.getAllValues();
assertThat(nestedSteps, hasSize(2));
nestedSteps.forEach(step -> assertThat(step.isHasStats(), equalTo(Boolean.FALSE)));
Set<String> nestedStepNames = nestedSteps.stream().map(StartTestItemRQ::getName).collect(Collectors.toSet());

assertThat(nestedStepNames, allOf(hasItem("Given def vara = 2"), hasItem("And def varb = 2")));
}
}
8 changes: 8 additions & 0 deletions src/test/resources/feature/background_two_steps.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Feature: test background with two steps reporting

Background: Set variable
Given def vara = 2
And def varb = 2

Scenario: Verify math
Then assert vara * varb == 4

0 comments on commit b5e86bb

Please sign in to comment.