diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cd28ba..bd9a0f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ## [Unreleased] ### Changed - Client version updated on [5.2.15](https://github.com/reportportal/client-java/releases/tag/5.2.15), by @HardNorth +### Fixed +- Issue [#38](https://github.com/reportportal/agent-java-karate/issues/38): Rerun failed scenarios, by @HardNorth ## [5.1.1] ### Fixed diff --git a/src/main/java/com/epam/reportportal/karate/ReportPortalHook.java b/src/main/java/com/epam/reportportal/karate/ReportPortalHook.java index 87dd831..a11386d 100644 --- a/src/main/java/com/epam/reportportal/karate/ReportPortalHook.java +++ b/src/main/java/com/epam/reportportal/karate/ReportPortalHook.java @@ -206,7 +206,7 @@ protected FinishTestItemRQ buildFinishFeatureRq(@Nonnull FeatureRuntime fr) { @Override public void afterFeature(FeatureRuntime fr) { - Optional> optionalId = ofNullable(featureIdMap.remove(getFeatureNameForReport(fr))).map(Supplier::get); + Optional> optionalId = ofNullable(featureIdMap.get(getFeatureNameForReport(fr))).map(Supplier::get); if (optionalId.isEmpty()) { LOGGER.error("ERROR: Trying to finish unspecified feature."); } @@ -239,11 +239,14 @@ protected StartTestItemRQ buildStartScenarioRq(@Nonnull ScenarioRuntime sr) { @Override public boolean beforeScenario(ScenarioRuntime sr) { StartTestItemRQ rq = buildStartScenarioRq(sr); - Optional> optionalId = ofNullable(featureIdMap.get(getFeatureNameForReport(sr.featureRuntime))).map( - Supplier::get); + Optional> optionalId = ofNullable(featureIdMap.get(getFeatureNameForReport(sr.featureRuntime))).map(Supplier::get); if (optionalId.isEmpty()) { LOGGER.error("ERROR: Trying to post unspecified feature."); } + ofNullable(scenarioIdMap.get(sr.scenario.getUniqueId())).map(Maybe::blockingGet).ifPresent(id -> { + rq.setRetry(true); + rq.setRetryOf(id); + }); optionalId.ifPresent(featureId -> { Maybe scenarioId = launch.get().startTestItem(featureId, rq); if (innerFeatures.contains(featureId) && StringUtils.isNotBlank(rq.getDescription())) { @@ -323,7 +326,7 @@ public void finishBackground(@Nullable StepResult stepResult, @Nonnull ScenarioR @Override public void afterScenario(ScenarioRuntime sr) { - Maybe scenarioId = scenarioIdMap.remove(sr.scenario.getUniqueId()); + Maybe scenarioId = scenarioIdMap.get(sr.scenario.getUniqueId()); if (scenarioId == null) { LOGGER.error("ERROR: Trying to finish unspecified scenario."); } diff --git a/src/test/java/com/epam/reportportal/karate/retry/RetryFailedTest.java b/src/test/java/com/epam/reportportal/karate/retry/RetryFailedTest.java new file mode 100644 index 0000000..1c5b591 --- /dev/null +++ b/src/test/java/com/epam/reportportal/karate/retry/RetryFailedTest.java @@ -0,0 +1,86 @@ +/* + * Copyright 2024 EPAM Systems + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.epam.reportportal.karate.retry; + +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.FinishTestItemRQ; +import com.epam.ta.reportportal.ws.model.StartTestItemRQ; +import com.intuit.karate.Results; +import com.intuit.karate.core.ScenarioResult; +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.Mockito.any; +import static org.mockito.Mockito.*; + +public class RetryFailedTest { + private static final String TEST_FEATURE = "classpath:feature/simple_failed.feature"; + private final String launchUuid = CommonUtils.namedId("launch_"); + private final String featureId = CommonUtils.namedId("feature_"); + private final List scenarioIds = Stream.generate(() -> CommonUtils.namedId("scenario_")).limit(2).collect(Collectors.toList()); + private final List stepIds = Stream.generate(() -> CommonUtils.namedId("step_")).limit(6).collect(Collectors.toList()); + + private final List>> scenarioSteps = Stream.of(Pair.of(scenarioIds.get(0), stepIds.subList(0, 3)), + Pair.of(scenarioIds.get(1), stepIds.subList(3, 6)) + ) + .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, launchUuid, featureId, scenarioSteps); + mockBatchLogging(client); + } + + @Test + public void test_simple_one_step_failed_retry() { + Results results = TestUtils.runAsHook(rp, TEST_FEATURE); + assertThat(results.getFailCount(), equalTo(1)); + + List failedResults = results.getScenarioResults().filter(ScenarioResult::isFailed).collect(Collectors.toList()); + assertThat(failedResults, hasSize(1)); + + results.getSuite().retryScenario(failedResults.get(0).getScenario()); + + verify(client).startTestItem(any(StartTestItemRQ.class)); + ArgumentCaptor scenarioStartCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class); + verify(client, times(2)).startTestItem(same(featureId), scenarioStartCaptor.capture()); + + verify(client).finishTestItem(same(featureId), any(FinishTestItemRQ.class)); + ArgumentCaptor scenarioCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); + verify(client).finishTestItem(same(scenarioIds.get(0)), scenarioCaptor.capture()); + verify(client).finishTestItem(same(scenarioIds.get(1)), scenarioCaptor.capture()); + + assertThat(scenarioStartCaptor.getAllValues().get(0).isRetry(), anyOf(equalTo(false), nullValue())); + assertThat(scenarioStartCaptor.getAllValues().get(1).isRetry(), equalTo(true)); + assertThat(scenarioStartCaptor.getAllValues().get(1).getRetryOf(), equalTo(scenarioIds.get(0))); + } +}