From ca6c8aa952f2897bf9127844a8bd498b34e9a764 Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Wed, 6 Nov 2024 15:03:56 +0300 Subject: [PATCH] Add more `@Issue` annotation tests --- .../junit5/ReportPortalExtension.java | 27 +-- .../junit5/IssueReportingTest.java | 161 ++++++++++++++++-- .../features/issue/DynamicIssueTest.java | 4 +- .../issue/ParameterizedWithOneIssueTest.java | 7 +- .../issue/ParameterizedWithTwoIssueTest.java | 3 +- .../features/issue/SimpleTwoIssuesTest.java | 5 +- .../features/issue/TwoDynamicIssueTest.java | 4 +- .../issue/TwoDynamicTwoIssueTest.java | 8 +- 8 files changed, 181 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/epam/reportportal/junit5/ReportPortalExtension.java b/src/main/java/com/epam/reportportal/junit5/ReportPortalExtension.java index 7177fa7..357343f 100644 --- a/src/main/java/com/epam/reportportal/junit5/ReportPortalExtension.java +++ b/src/main/java/com/epam/reportportal/junit5/ReportPortalExtension.java @@ -468,19 +468,20 @@ protected void startTestItem(@Nonnull final ExtensionContext context, @Nonnull f idMapping.computeIfAbsent(context, c -> { StartTestItemRQ rq = buildStartStepRq(c, arguments, itemType, description, startTime); Launch launch = getLaunch(c); - Maybe itemId = c.getParent().flatMap(parent -> Optional.ofNullable(idMapping.get(parent))).map(parentTest -> { - Maybe item = launch.startTestItem(parentTest, rq); - if (getReporter().getParameters().isCallbackReportingEnabled()) { - TEST_ITEM_TREE.getTestItems().put(createItemTreeKey(rq.getName()), createTestItemLeaf(parentTest, item)); - } - return item; - }).orElseGet(() -> { - Maybe item = launch.startTestItem(rq); - if (getReporter().getParameters().isCallbackReportingEnabled()) { - TEST_ITEM_TREE.getTestItems().put(createItemTreeKey(rq.getName()), createTestItemLeaf(item)); - } - return item; - }); + + Maybe parentId = c.getParent().flatMap(parent -> Optional.ofNullable(idMapping.get(parent))).orElse(null); + Maybe itemId; + TestItemTree.TestItemLeaf leaf; + if (parentId == null) { + itemId = launch.startTestItem(rq); + leaf = createTestItemLeaf(itemId); + } else { + itemId = launch.startTestItem(parentId, rq); + leaf = createTestItemLeaf(parentId, itemId); + } + if (getReporter().getParameters().isCallbackReportingEnabled()) { + TEST_ITEM_TREE.getTestItems().put(createItemTreeKey(rq.getName()), leaf); + } if (TEMPLATE == itemType) { testTemplates.put(c, itemId); } diff --git a/src/test/java/com/epam/reportportal/junit5/IssueReportingTest.java b/src/test/java/com/epam/reportportal/junit5/IssueReportingTest.java index 9412a79..e8c5e14 100644 --- a/src/test/java/com/epam/reportportal/junit5/IssueReportingTest.java +++ b/src/test/java/com/epam/reportportal/junit5/IssueReportingTest.java @@ -16,7 +16,7 @@ package com.epam.reportportal.junit5; -import com.epam.reportportal.junit5.features.issue.SimpleIssueTest; +import com.epam.reportportal.junit5.features.issue.*; import com.epam.reportportal.junit5.util.TestUtils; import com.epam.reportportal.listeners.ItemStatus; import com.epam.reportportal.service.Launch; @@ -32,13 +32,16 @@ import org.mockito.ArgumentCaptor; import org.mockito.stubbing.Answer; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.Queue; + import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; -@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@TestInstance(TestInstance.Lifecycle.PER_METHOD) public class IssueReportingTest { public static class TestExtension extends ReportPortalExtension { static Launch LAUNCH; @@ -49,17 +52,24 @@ protected Launch getLaunch(ExtensionContext context) { } } - private final String SUITE_ID = CommonUtils.namedId("suite_"); - private final Maybe SUITE_MAYBE = Maybe.just(SUITE_ID); - private final String STEP_ID = CommonUtils.namedId("step_"); - private final Maybe STEP_MAYBE = Maybe.just(STEP_ID); + private final String suiteId = CommonUtils.namedId("suite_"); + private final Maybe suiteMaybe = Maybe.just(suiteId); + private final String stepOneId = CommonUtils.namedId("step_"); + private final Maybe stepOneMaybe = Maybe.just(stepOneId); + private final String stepTwoId = CommonUtils.namedId("step_"); + private final Maybe stepTwoMaybe = Maybe.just(stepTwoId); + private final String stepThreeId = CommonUtils.namedId("step_"); + private final Maybe stepThreeMaybe = Maybe.just(stepThreeId); + private final Queue> stepIds = new LinkedList<>(Arrays.asList(stepOneMaybe, stepTwoMaybe, stepThreeMaybe)); @BeforeEach public void setupMock() { Launch launch = mock(Launch.class); IssueReportingTest.TestExtension.LAUNCH = launch; - when(launch.startTestItem(any())).thenAnswer((Answer>) invocation -> SUITE_MAYBE); - when(launch.startTestItem(same(SUITE_MAYBE), any())).thenAnswer((Answer>) invocation -> STEP_MAYBE); + when(launch.startTestItem(any())).thenAnswer((Answer>) invocation -> suiteMaybe); + when(launch.startTestItem(any(), any())).thenAnswer((Answer>) invocation -> CommonUtils.createMaybeUuid()); + when(launch.startTestItem(same(suiteMaybe), any())).thenAnswer((Answer>) invocation -> stepIds.poll()); + when(launch.startTestItem(same(stepOneMaybe), any())).thenAnswer((Answer>) invocation -> stepIds.poll()); when(launch.finishTestItem(any(), any() )).thenAnswer((Answer>) invocation -> Maybe.just(new OperationCompletionRS("OK"))); @@ -71,7 +81,7 @@ public void verify_simple_test_failure() { Launch launch = IssueReportingTest.TestExtension.LAUNCH; ArgumentCaptor testCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); - verify(launch).finishTestItem(same(STEP_MAYBE), testCaptor.capture()); + verify(launch).finishTestItem(same(stepOneMaybe), testCaptor.capture()); FinishTestItemRQ finishTestItemRQ = testCaptor.getValue(); assertThat(finishTestItemRQ.getStatus(), equalTo(ItemStatus.FAILED.name())); @@ -80,4 +90,133 @@ public void verify_simple_test_failure() { assertThat(issue.getIssueType(), equalTo("pb001")); assertThat(issue.getComment(), equalTo(SimpleIssueTest.FAILURE_MESSAGE)); } + + @Test + public void verify_test_failure_with_two_issues() { + TestUtils.runClasses(SimpleTwoIssuesTest.class); + + Launch launch = IssueReportingTest.TestExtension.LAUNCH; + ArgumentCaptor testCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); + verify(launch).finishTestItem(same(stepOneMaybe), testCaptor.capture()); + + FinishTestItemRQ finishTestItemRQ = testCaptor.getValue(); + assertThat(finishTestItemRQ.getStatus(), equalTo(ItemStatus.FAILED.name())); + assertThat(finishTestItemRQ.getIssue(), notNullValue()); + Issue issue = finishTestItemRQ.getIssue(); + assertThat(issue.getIssueType(), equalTo("ab001")); + assertThat(issue.getComment(), equalTo(SimpleTwoIssuesTest.FAILURE_MESSAGE)); + } + + @Test + public void verify_parameterized_test_failure_with_one_issue() { + TestUtils.runClasses(ParameterizedWithOneIssueTest.class); + + Launch launch = IssueReportingTest.TestExtension.LAUNCH; + ArgumentCaptor firstTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); + verify(launch).finishTestItem(same(stepTwoMaybe), firstTestCaptor.capture()); + ArgumentCaptor secondTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); + verify(launch).finishTestItem(same(stepThreeMaybe), secondTestCaptor.capture()); + + FinishTestItemRQ finishTestItemRQ = firstTestCaptor.getValue(); + assertThat(finishTestItemRQ.getStatus(), equalTo(ItemStatus.FAILED.name())); + assertThat(finishTestItemRQ.getIssue(), nullValue()); + + finishTestItemRQ = secondTestCaptor.getValue(); + assertThat(finishTestItemRQ.getStatus(), equalTo(ItemStatus.FAILED.name())); + assertThat(finishTestItemRQ.getIssue(), notNullValue()); + Issue issue = finishTestItemRQ.getIssue(); + assertThat(issue.getIssueType(), equalTo("ab001")); + assertThat(issue.getComment(), equalTo(ParameterizedWithOneIssueTest.ISSUE_MESSAGE)); + } + + @Test + public void verify_parameterized_test_failure_with_two_issues() { + TestUtils.runClasses(ParameterizedWithTwoIssueTest.class); + + Launch launch = IssueReportingTest.TestExtension.LAUNCH; + ArgumentCaptor firstTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); + verify(launch).finishTestItem(same(stepTwoMaybe), firstTestCaptor.capture()); + ArgumentCaptor secondTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); + verify(launch).finishTestItem(same(stepThreeMaybe), secondTestCaptor.capture()); + + FinishTestItemRQ finishTestItemRQ = firstTestCaptor.getValue(); + assertThat(finishTestItemRQ.getStatus(), equalTo(ItemStatus.FAILED.name())); + assertThat(finishTestItemRQ.getIssue(), notNullValue()); + Issue issue = finishTestItemRQ.getIssue(); + assertThat(issue.getIssueType(), equalTo("ab001")); + assertThat(issue.getComment(), equalTo(ParameterizedWithTwoIssueTest.ISSUE_MESSAGE)); + + finishTestItemRQ = secondTestCaptor.getValue(); + assertThat(finishTestItemRQ.getStatus(), equalTo(ItemStatus.FAILED.name())); + assertThat(finishTestItemRQ.getIssue(), notNullValue()); + issue = finishTestItemRQ.getIssue(); + assertThat(issue.getIssueType(), equalTo("pb001")); + assertThat(issue.getComment(), equalTo(ParameterizedWithTwoIssueTest.ISSUE_MESSAGE)); + } + + @Test + public void verify_dynamic_test_failure() { + TestUtils.runClasses(DynamicIssueTest.class); + + Launch launch = IssueReportingTest.TestExtension.LAUNCH; + ArgumentCaptor testCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); + verify(launch).finishTestItem(same(stepTwoMaybe), testCaptor.capture()); + + FinishTestItemRQ finishTestItemRQ = testCaptor.getValue(); + assertThat(finishTestItemRQ.getStatus(), equalTo(ItemStatus.FAILED.name())); + assertThat(finishTestItemRQ.getIssue(), notNullValue()); + Issue issue = finishTestItemRQ.getIssue(); + assertThat(issue.getIssueType(), equalTo("ab001")); + assertThat(issue.getComment(), equalTo(DynamicIssueTest.FAILURE_MESSAGE)); + } + + @Test + public void verify_two_dynamic_test_failures() { + TestUtils.runClasses(TwoDynamicIssueTest.class); + + Launch launch = IssueReportingTest.TestExtension.LAUNCH; + ArgumentCaptor firstTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); + verify(launch).finishTestItem(same(stepTwoMaybe), firstTestCaptor.capture()); + ArgumentCaptor secondTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); + verify(launch).finishTestItem(same(stepThreeMaybe), secondTestCaptor.capture()); + + FinishTestItemRQ finishTestItemRQ = firstTestCaptor.getValue(); + assertThat(finishTestItemRQ.getStatus(), equalTo(ItemStatus.FAILED.name())); + assertThat(finishTestItemRQ.getIssue(), notNullValue()); + Issue issue = finishTestItemRQ.getIssue(); + assertThat(issue.getIssueType(), equalTo("ab001")); + assertThat(issue.getComment(), equalTo(TwoDynamicIssueTest.FAILURE_MESSAGE)); + + finishTestItemRQ = secondTestCaptor.getValue(); + assertThat(finishTestItemRQ.getStatus(), equalTo(ItemStatus.FAILED.name())); + assertThat(finishTestItemRQ.getIssue(), notNullValue()); + issue = finishTestItemRQ.getIssue(); + assertThat(issue.getIssueType(), equalTo("ab001")); + assertThat(issue.getComment(), equalTo(TwoDynamicIssueTest.FAILURE_MESSAGE)); + } + + @Test + public void verify_two_dynamic_test_failures_two_issues() { + TestUtils.runClasses(TwoDynamicTwoIssueTest.class); + + Launch launch = IssueReportingTest.TestExtension.LAUNCH; + ArgumentCaptor firstTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); + verify(launch).finishTestItem(same(stepTwoMaybe), firstTestCaptor.capture()); + ArgumentCaptor secondTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); + verify(launch).finishTestItem(same(stepThreeMaybe), secondTestCaptor.capture()); + + FinishTestItemRQ finishTestItemRQ = firstTestCaptor.getValue(); + assertThat(finishTestItemRQ.getStatus(), equalTo(ItemStatus.FAILED.name())); + assertThat(finishTestItemRQ.getIssue(), notNullValue()); + Issue issue = finishTestItemRQ.getIssue(); + assertThat(issue.getIssueType(), equalTo("ab001")); + assertThat(issue.getComment(), equalTo(TwoDynamicTwoIssueTest.FAILURE_MESSAGE)); + + finishTestItemRQ = secondTestCaptor.getValue(); + assertThat(finishTestItemRQ.getStatus(), equalTo(ItemStatus.FAILED.name())); + assertThat(finishTestItemRQ.getIssue(), notNullValue()); + issue = finishTestItemRQ.getIssue(); + assertThat(issue.getIssueType(), equalTo("pb001")); + assertThat(issue.getComment(), equalTo(TwoDynamicTwoIssueTest.FAILURE_MESSAGE)); + } } diff --git a/src/test/java/com/epam/reportportal/junit5/features/issue/DynamicIssueTest.java b/src/test/java/com/epam/reportportal/junit5/features/issue/DynamicIssueTest.java index 1ba9688..875cc43 100644 --- a/src/test/java/com/epam/reportportal/junit5/features/issue/DynamicIssueTest.java +++ b/src/test/java/com/epam/reportportal/junit5/features/issue/DynamicIssueTest.java @@ -1,7 +1,7 @@ package com.epam.reportportal.junit5.features.issue; import com.epam.reportportal.annotations.Issue; -import com.epam.reportportal.junit5.DisplayNameTest; +import com.epam.reportportal.junit5.IssueReportingTest; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.TestFactory; import org.junit.jupiter.api.extension.ExtendWith; @@ -10,7 +10,7 @@ import static org.junit.jupiter.api.DynamicTest.dynamicTest; -@ExtendWith(DisplayNameTest.TestExtension.class) +@ExtendWith(IssueReportingTest.TestExtension.class) public class DynamicIssueTest { public static final String FAILURE_MESSAGE = "This test is expected to fail"; diff --git a/src/test/java/com/epam/reportportal/junit5/features/issue/ParameterizedWithOneIssueTest.java b/src/test/java/com/epam/reportportal/junit5/features/issue/ParameterizedWithOneIssueTest.java index e345dac..1010f5c 100644 --- a/src/test/java/com/epam/reportportal/junit5/features/issue/ParameterizedWithOneIssueTest.java +++ b/src/test/java/com/epam/reportportal/junit5/features/issue/ParameterizedWithOneIssueTest.java @@ -19,19 +19,20 @@ import com.epam.reportportal.annotations.Issue; import com.epam.reportportal.annotations.TestFilter; import com.epam.reportportal.annotations.TestParamFilter; +import com.epam.reportportal.junit5.IssueReportingTest; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -@ExtendWith(com.epam.reportportal.junit5.ReportPortalExtension.class) +@ExtendWith(IssueReportingTest.TestExtension.class) public class ParameterizedWithOneIssueTest { public static final String FAILURE_MESSAGE = "This parameterized test is expected to fail: "; + public static final String ISSUE_MESSAGE = "This test is expected to fail"; @ParameterizedTest @ValueSource(booleans = { true, false }) - @Issue(value = "ab001", comment = "This test is expected to fail", filter = @TestFilter(param = { - @TestParamFilter(valueStartsWith = "true") })) + @Issue(value = "ab001", comment = ISSUE_MESSAGE, filter = @TestFilter(param = { @TestParamFilter(valueStartsWith = "false") })) public void failureTest(boolean param) { throw new IllegalStateException(FAILURE_MESSAGE + param); } diff --git a/src/test/java/com/epam/reportportal/junit5/features/issue/ParameterizedWithTwoIssueTest.java b/src/test/java/com/epam/reportportal/junit5/features/issue/ParameterizedWithTwoIssueTest.java index c368b8e..4d156c4 100644 --- a/src/test/java/com/epam/reportportal/junit5/features/issue/ParameterizedWithTwoIssueTest.java +++ b/src/test/java/com/epam/reportportal/junit5/features/issue/ParameterizedWithTwoIssueTest.java @@ -19,11 +19,12 @@ import com.epam.reportportal.annotations.Issue; import com.epam.reportportal.annotations.TestFilter; import com.epam.reportportal.annotations.TestParamFilter; +import com.epam.reportportal.junit5.IssueReportingTest; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -@ExtendWith(com.epam.reportportal.junit5.ReportPortalExtension.class) +@ExtendWith(IssueReportingTest.TestExtension.class) public class ParameterizedWithTwoIssueTest { public static final String FAILURE_MESSAGE = "This parameterized test is expected to fail: "; diff --git a/src/test/java/com/epam/reportportal/junit5/features/issue/SimpleTwoIssuesTest.java b/src/test/java/com/epam/reportportal/junit5/features/issue/SimpleTwoIssuesTest.java index 1714ed1..562e2f8 100644 --- a/src/test/java/com/epam/reportportal/junit5/features/issue/SimpleTwoIssuesTest.java +++ b/src/test/java/com/epam/reportportal/junit5/features/issue/SimpleTwoIssuesTest.java @@ -17,17 +17,18 @@ package com.epam.reportportal.junit5.features.issue; import com.epam.reportportal.annotations.Issue; +import com.epam.reportportal.junit5.IssueReportingTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -@ExtendWith(com.epam.reportportal.junit5.ReportPortalExtension.class) +@ExtendWith(IssueReportingTest.TestExtension.class) public class SimpleTwoIssuesTest { public static final String FAILURE_MESSAGE = "This test is expected to fail"; @Test - @Issue(value = "pb001", comment = FAILURE_MESSAGE) @Issue(value = "ab001", comment = FAILURE_MESSAGE) + @Issue(value = "pb001", comment = FAILURE_MESSAGE) public void failureTest() { throw new IllegalStateException(FAILURE_MESSAGE); } diff --git a/src/test/java/com/epam/reportportal/junit5/features/issue/TwoDynamicIssueTest.java b/src/test/java/com/epam/reportportal/junit5/features/issue/TwoDynamicIssueTest.java index e9322a7..cda7051 100644 --- a/src/test/java/com/epam/reportportal/junit5/features/issue/TwoDynamicIssueTest.java +++ b/src/test/java/com/epam/reportportal/junit5/features/issue/TwoDynamicIssueTest.java @@ -1,7 +1,7 @@ package com.epam.reportportal.junit5.features.issue; import com.epam.reportportal.annotations.Issue; -import com.epam.reportportal.junit5.DisplayNameTest; +import com.epam.reportportal.junit5.IssueReportingTest; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.TestFactory; import org.junit.jupiter.api.extension.ExtendWith; @@ -10,7 +10,7 @@ import static org.junit.jupiter.api.DynamicTest.dynamicTest; -@ExtendWith(DisplayNameTest.TestExtension.class) +@ExtendWith(IssueReportingTest.TestExtension.class) public class TwoDynamicIssueTest { public static final String FAILURE_MESSAGE = "This test is expected to fail"; diff --git a/src/test/java/com/epam/reportportal/junit5/features/issue/TwoDynamicTwoIssueTest.java b/src/test/java/com/epam/reportportal/junit5/features/issue/TwoDynamicTwoIssueTest.java index 8667c0b..442c6db 100644 --- a/src/test/java/com/epam/reportportal/junit5/features/issue/TwoDynamicTwoIssueTest.java +++ b/src/test/java/com/epam/reportportal/junit5/features/issue/TwoDynamicTwoIssueTest.java @@ -3,7 +3,7 @@ import com.epam.reportportal.annotations.Issue; import com.epam.reportportal.annotations.TestFilter; import com.epam.reportportal.annotations.TestNameFilter; -import com.epam.reportportal.junit5.DisplayNameTest; +import com.epam.reportportal.junit5.IssueReportingTest; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.TestFactory; import org.junit.jupiter.api.extension.ExtendWith; @@ -12,13 +12,13 @@ import static org.junit.jupiter.api.DynamicTest.dynamicTest; -@ExtendWith(DisplayNameTest.TestExtension.class) +@ExtendWith(IssueReportingTest.TestExtension.class) public class TwoDynamicTwoIssueTest { public static final String FAILURE_MESSAGE = "This test is expected to fail"; @TestFactory - @Issue(value = "ab001", comment = FAILURE_MESSAGE, filter = {@TestFilter(name = @TestNameFilter(endsWith = "test")) }) - @Issue(value = "pb001", comment = FAILURE_MESSAGE, filter = {@TestFilter(name = @TestNameFilter(contains = "test 2")) }) + @Issue(value = "ab001", comment = FAILURE_MESSAGE, filter = { @TestFilter(name = @TestNameFilter(endsWith = "test")) }) + @Issue(value = "pb001", comment = FAILURE_MESSAGE, filter = { @TestFilter(name = @TestNameFilter(contains = "test 2")) }) Stream testForTestFactory() { return Stream.of(dynamicTest("My dynamic test", () -> { throw new IllegalStateException(FAILURE_MESSAGE);