Skip to content

Commit

Permalink
Add more @Issue annotation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Nov 6, 2024
1 parent c6c6ea7 commit ca6c8aa
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> itemId = c.getParent().flatMap(parent -> Optional.ofNullable(idMapping.get(parent))).map(parentTest -> {
Maybe<String> item = launch.startTestItem(parentTest, rq);
if (getReporter().getParameters().isCallbackReportingEnabled()) {
TEST_ITEM_TREE.getTestItems().put(createItemTreeKey(rq.getName()), createTestItemLeaf(parentTest, item));
}
return item;
}).orElseGet(() -> {
Maybe<String> item = launch.startTestItem(rq);
if (getReporter().getParameters().isCallbackReportingEnabled()) {
TEST_ITEM_TREE.getTestItems().put(createItemTreeKey(rq.getName()), createTestItemLeaf(item));
}
return item;
});

Maybe<String> parentId = c.getParent().flatMap(parent -> Optional.ofNullable(idMapping.get(parent))).orElse(null);
Maybe<String> 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);
}
Expand Down
161 changes: 150 additions & 11 deletions src/test/java/com/epam/reportportal/junit5/IssueReportingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -49,17 +52,24 @@ protected Launch getLaunch(ExtensionContext context) {
}
}

private final String SUITE_ID = CommonUtils.namedId("suite_");
private final Maybe<String> SUITE_MAYBE = Maybe.just(SUITE_ID);
private final String STEP_ID = CommonUtils.namedId("step_");
private final Maybe<String> STEP_MAYBE = Maybe.just(STEP_ID);
private final String suiteId = CommonUtils.namedId("suite_");
private final Maybe<String> suiteMaybe = Maybe.just(suiteId);
private final String stepOneId = CommonUtils.namedId("step_");
private final Maybe<String> stepOneMaybe = Maybe.just(stepOneId);
private final String stepTwoId = CommonUtils.namedId("step_");
private final Maybe<String> stepTwoMaybe = Maybe.just(stepTwoId);
private final String stepThreeId = CommonUtils.namedId("step_");
private final Maybe<String> stepThreeMaybe = Maybe.just(stepThreeId);
private final Queue<Maybe<String>> 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<Maybe<String>>) invocation -> SUITE_MAYBE);
when(launch.startTestItem(same(SUITE_MAYBE), any())).thenAnswer((Answer<Maybe<String>>) invocation -> STEP_MAYBE);
when(launch.startTestItem(any())).thenAnswer((Answer<Maybe<String>>) invocation -> suiteMaybe);
when(launch.startTestItem(any(), any())).thenAnswer((Answer<Maybe<String>>) invocation -> CommonUtils.createMaybeUuid());
when(launch.startTestItem(same(suiteMaybe), any())).thenAnswer((Answer<Maybe<String>>) invocation -> stepIds.poll());
when(launch.startTestItem(same(stepOneMaybe), any())).thenAnswer((Answer<Maybe<String>>) invocation -> stepIds.poll());
when(launch.finishTestItem(any(),
any()
)).thenAnswer((Answer<Maybe<OperationCompletionRS>>) invocation -> Maybe.just(new OperationCompletionRS("OK")));
Expand All @@ -71,7 +81,7 @@ public void verify_simple_test_failure() {

Launch launch = IssueReportingTest.TestExtension.LAUNCH;
ArgumentCaptor<FinishTestItemRQ> 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()));
Expand All @@ -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<FinishTestItemRQ> 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<FinishTestItemRQ> firstTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class);
verify(launch).finishTestItem(same(stepTwoMaybe), firstTestCaptor.capture());
ArgumentCaptor<FinishTestItemRQ> 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<FinishTestItemRQ> firstTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class);
verify(launch).finishTestItem(same(stepTwoMaybe), firstTestCaptor.capture());
ArgumentCaptor<FinishTestItemRQ> 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<FinishTestItemRQ> 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<FinishTestItemRQ> firstTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class);
verify(launch).finishTestItem(same(stepTwoMaybe), firstTestCaptor.capture());
ArgumentCaptor<FinishTestItemRQ> 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<FinishTestItemRQ> firstTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class);
verify(launch).finishTestItem(same(stepTwoMaybe), firstTestCaptor.capture());
ArgumentCaptor<FinishTestItemRQ> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<DynamicTest> testForTestFactory() {
return Stream.of(dynamicTest("My dynamic test", () -> {
throw new IllegalStateException(FAILURE_MESSAGE);
Expand Down

0 comments on commit ca6c8aa

Please sign in to comment.