From d51985cd2f93f2b2095708f0d7395ec354239196 Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Fri, 22 Dec 2023 12:48:18 +0300 Subject: [PATCH] Add another test --- .../background/BackgroundExamplesTest.java | 97 +++++++++++++++++++ .../feature/background_examples.feature | 13 +++ 2 files changed, 110 insertions(+) create mode 100644 src/test/java/com/epam/reportportal/karate/background/BackgroundExamplesTest.java create mode 100644 src/test/resources/feature/background_examples.feature diff --git a/src/test/java/com/epam/reportportal/karate/background/BackgroundExamplesTest.java b/src/test/java/com/epam/reportportal/karate/background/BackgroundExamplesTest.java new file mode 100644 index 0000000..e29f9ab --- /dev/null +++ b/src/test/java/com/epam/reportportal/karate/background/BackgroundExamplesTest.java @@ -0,0 +1,97 @@ +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 BackgroundExamplesTest { + private final String featureId = CommonUtils.namedId("feature_"); + private final List scenarioIds = Stream.generate(() -> CommonUtils.namedId("scenario_")) + .limit(2).collect(Collectors.toList()); + + private final List>> scenarioSteps = scenarioIds + .stream().map(s -> + Pair.of(s, Stream.generate(() -> + CommonUtils.namedId("step_")).limit(3).collect(Collectors.toList()))) + .collect(Collectors.toList()); + private final List> nestedStepIds = scenarioSteps + .stream() + .flatMap(s -> s.getValue().stream()) + .map(s -> Pair.of(s, 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, scenarioSteps); + mockNestedSteps(client, nestedStepIds); + mockBatchLogging(client); + } + + @Test + public void test_background_steps() { + var results = TestUtils.runAsReport(rp, "classpath:feature/background_examples.feature"); + assertThat(results.getFailCount(), equalTo(0)); + + ArgumentCaptor captor = ArgumentCaptor.forClass(StartTestItemRQ.class); + verify(client).startTestItem(captor.capture()); + verify(client, times(2)).startTestItem(same(featureId), captor.capture()); + ArgumentCaptor firstStepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class); + verify(client, times(3)).startTestItem(same(scenarioIds.get(0)), firstStepCaptor.capture()); + ArgumentCaptor secondStepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class); + verify(client, times(3)).startTestItem(same(scenarioIds.get(1)), secondStepCaptor.capture()); + ArgumentCaptor nestedStepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class); + verify(client, times(2)).startTestItem(startsWith("step_"), nestedStepCaptor.capture()); + + List items = captor.getAllValues(); + assertThat(items, hasSize(3)); + List firstSteps = firstStepCaptor.getAllValues(); + assertThat(firstSteps, hasSize(3)); + List secondSteps = secondStepCaptor.getAllValues(); + assertThat(firstSteps, hasSize(3)); + + List firstBackgroundSteps = firstSteps.stream() + .filter(s -> s.getName().startsWith(Background.KEYWORD)).collect(Collectors.toList()); + assertThat(firstBackgroundSteps, hasSize(1)); + List secondBackgroundSteps = secondSteps.stream() + .filter(s -> s.getName().startsWith(Background.KEYWORD)).collect(Collectors.toList()); + assertThat(secondBackgroundSteps, hasSize(1)); + + Stream.concat(firstBackgroundSteps.stream(), secondBackgroundSteps.stream()).forEach(backgroundStep ->{ + 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 nestedSteps = nestedStepCaptor.getAllValues(); + assertThat(nestedSteps, hasSize(2)); + nestedSteps.forEach(step -> assertThat(step.isHasStats(), equalTo(Boolean.FALSE))); + Set nestedStepNames = nestedSteps.stream().map(StartTestItemRQ::getName).collect(Collectors.toSet()); + + assertThat(nestedStepNames, hasSize(1)); + assertThat(nestedStepNames, hasItem("Given def varb = 2")); + } +} diff --git a/src/test/resources/feature/background_examples.feature b/src/test/resources/feature/background_examples.feature new file mode 100644 index 0000000..b65a603 --- /dev/null +++ b/src/test/resources/feature/background_examples.feature @@ -0,0 +1,13 @@ +Feature: test background with Scenario Outline reporting + + Background: Set varb + Given def varb = 2 + + Scenario Outline: Verify different maths + Given def mathResult = vara + varb + Then assert mathResult == result + + Examples: + | vara! | result! | + | 2 | 4 | + | 1 | 3 |