Skip to content

Commit

Permalink
Add Test Case ID
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Dec 15, 2023
1 parent 9413642 commit b2587fe
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.epam.reportportal.listeners.ListenerParameters;
import com.epam.reportportal.service.Launch;
import com.epam.reportportal.service.ReportPortal;
import com.epam.reportportal.service.item.TestCaseIdEntry;
import com.epam.reportportal.utils.MemoizingSupplier;
import com.epam.reportportal.utils.TestCaseIdUtils;
import com.epam.ta.reportportal.ws.model.FinishExecutionRQ;
import com.epam.ta.reportportal.ws.model.FinishTestItemRQ;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
Expand All @@ -18,6 +20,7 @@
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
Expand All @@ -26,6 +29,7 @@
import java.util.function.Supplier;

import static com.google.common.base.Strings.isNullOrEmpty;
import static java.util.Optional.ofNullable;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

public class ReportPortalPublisher {
Expand Down Expand Up @@ -112,8 +116,15 @@ public void finishLaunch() {
Runtime.getRuntime().removeShutdownHook(shutDownHook);
}

protected String getCodeRef(Scenario scenario) {
if(scenario.getExampleIndex() < 0) {
/**
* Returns code reference for feature files by URI and Scenario reference
*
* @param scenario Karate's Scenario object instance
* @return a code reference
*/
@Nonnull
protected String getCodeRef(@Nonnull Scenario scenario) {
if (scenario.getExampleIndex() < 0) {
return String.format(SCENARIO_CODE_REFERENCE_PATTERN, scenario.getFeature().getResource().getRelativePath(),
scenario.getName());
} else {
Expand All @@ -122,6 +133,17 @@ protected String getCodeRef(Scenario scenario) {
}
}

/**
* Return a Test Case ID for a Scenario in a Feature file
*
* @param scenario Karate's Scenario object instance
* @return Test Case ID entity or null if it's not possible to calculate
*/
@Nullable
protected TestCaseIdEntry getTestCaseId(@Nonnull Scenario scenario) {
return TestCaseIdUtils.getTestCaseId(getCodeRef(scenario), null);
}

/**
* Customize start test item event/request
*
Expand Down Expand Up @@ -166,7 +188,9 @@ protected StartTestItemRQ buildStartScenarioRq(@Nonnull ScenarioResult scenarioR
StartTestItemRQ rq = buildStartTestItemRq(scenarioResult.getScenario().getName(),
Calendar.getInstance().getTime(),
ItemType.STEP);
rq.setCodeRef(getCodeRef(scenarioResult.getScenario()));
Scenario scenario = scenarioResult.getScenario();
rq.setCodeRef(getCodeRef(scenario));
rq.setTestCaseId(ofNullable(getTestCaseId(scenario)).map(TestCaseIdEntry::getId).orElse(null));
return rq;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.epam.reportportal.karate.id;

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.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 ExamplesTestCaseIdTest {
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_TEST_CASE_ID_PATTERN =
"feature/examples.feature/[EXAMPLE:Verify different maths[%s]]";
private static final String FIRST_EXAMPLE_TEST_CASE_ID =
String.format(EXAMPLE_TEST_CASE_ID_PATTERN, "result:4;vara:2;varb:2");
private static final String SECOND_EXAMPLE_TEST_CASE_ID =
String.format(EXAMPLE_TEST_CASE_ID_PATTERN, "result:3;vara:1;varb:2");

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_test_case_id() {
var results = TestUtils.runAsReport(rp, "classpath:feature/examples.feature");
assertThat(results.getFailCount(), equalTo(0));

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

List<StartTestItemRQ> items = captor.getAllValues();
assertThat(items, hasSize(7));

StartTestItemRQ firstScenarioRq = items.get(1);
assertThat(firstScenarioRq.getTestCaseId(), allOf(notNullValue(), equalTo(FIRST_EXAMPLE_TEST_CASE_ID)));

StartTestItemRQ secondScenarioRq = items.get(2);
assertThat(secondScenarioRq.getTestCaseId(), allOf(notNullValue(), equalTo(SECOND_EXAMPLE_TEST_CASE_ID)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.epam.reportportal.karate.id;

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.*;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.*;

public class ScenarioTestCaseIdTest {
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 static final String SIMPLE_TEST_CASE_ID = "feature/simple.feature/[SCENARIO:Verify math]";

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_test_case_id() {
var results = TestUtils.runAsReport(rp, "classpath:feature/simple.feature");
assertThat(results.getFailCount(), equalTo(0));

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

List<StartTestItemRQ> items = captor.getAllValues();
assertThat(items, hasSize(5));

StartTestItemRQ scenarioRq = items.get(1);
assertThat(scenarioRq.getTestCaseId(), allOf(notNullValue(), equalTo(SIMPLE_TEST_CASE_ID)));
}
}

0 comments on commit b2587fe

Please sign in to comment.