-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from aykhangaffarov/RPP_LAST_ERROR_LOG_DESCRI…
…PTION Add last error in test description
- Loading branch information
Showing
6 changed files
with
261 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
src/test/java/com/epam/reportportal/junit5/ErrorLastLogTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package com.epam.reportportal.junit5; | ||
|
||
import com.epam.reportportal.junit5.features.lasterrorlog.ErrorLastLogFeatureWithAssertionErrorTest; | ||
import com.epam.reportportal.junit5.features.lasterrorlog.ErrorLastLogFeatureWithAssertionPassedTest; | ||
import com.epam.reportportal.junit5.features.lasterrorlog.ErrorLastLogFeatureWithExceptionTest; | ||
import com.epam.reportportal.junit5.features.lasterrorlog.ErrorLastLogFeatureWithStepTest; | ||
import com.epam.reportportal.junit5.util.TestUtils; | ||
import com.epam.reportportal.service.Launch; | ||
import com.epam.reportportal.service.ReportPortal; | ||
import com.epam.reportportal.service.ReportPortalClient; | ||
import com.epam.ta.reportportal.ws.model.FinishTestItemRQ; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.platform.engine.TestExecutionResult; | ||
import org.junit.platform.launcher.TestExecutionListener; | ||
import org.junit.platform.launcher.TestIdentifier; | ||
import org.mockito.ArgumentCaptor; | ||
|
||
import java.util.Deque; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentLinkedDeque; | ||
|
||
import static com.epam.reportportal.util.test.CommonUtils.namedId; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.startsWith; | ||
import static org.mockito.Mockito.*; | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
public class ErrorLastLogTest { | ||
|
||
private final String testErrorMessagePattern = "%s\nError: \n%s"; | ||
private final String assertErrorMessage = "org.opentest4j.AssertionFailedError: expected: <0> but was: <1>"; | ||
private final String exceptionStepErrorMessage = "java.util.NoSuchElementException: Error message"; | ||
private final String testExceptionMessage = "java.lang.RuntimeException: Critical error"; | ||
private final String assertDescriptionMessage = "0 and 1 is not equal"; | ||
private final String stepDescriptionMessage = "Login issue"; | ||
private final String failedStatus = "FAILED"; | ||
private final String passedStatus = "PASSED"; | ||
static final String testClassUuid = namedId("class"); | ||
static final String testMethodUuid = namedId("test"); | ||
public static class ErrorDescriptionTestExtension extends ReportPortalExtension { | ||
|
||
static final ThreadLocal<ReportPortalClient> client = new ThreadLocal<>(); | ||
static final ThreadLocal<Launch> launch = new ThreadLocal<>(); | ||
public static void init() { | ||
client.set(mock(ReportPortalClient.class)); | ||
TestUtils.mockLaunch(client.get(), "launchUuid", testClassUuid, testMethodUuid); | ||
TestUtils.mockLogging(client.get()); | ||
ReportPortal reportPortal = ReportPortal.create(client.get(), TestUtils.standardParameters()); | ||
launch.set(reportPortal.newLaunch(TestUtils.launchRQ(reportPortal.getParameters()))); | ||
|
||
} | ||
|
||
@Override | ||
protected Launch getLaunch(ExtensionContext context) { | ||
return launch.get(); | ||
} | ||
|
||
} | ||
|
||
public static class Listener implements TestExecutionListener { | ||
public Deque<TestExecutionResult> results = new ConcurrentLinkedDeque<>(); | ||
|
||
@Override | ||
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { | ||
if (testIdentifier.isTest()) { | ||
results.add(testExecutionResult); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void verify_last_error_log_exception() { | ||
ErrorDescriptionTestExtension.init(); | ||
Listener listener = new Listener(); | ||
TestUtils.runClasses(listener, ErrorLastLogFeatureWithExceptionTest.class); | ||
|
||
ReportPortalClient client = ErrorDescriptionTestExtension.client.get(); | ||
|
||
ArgumentCaptor<FinishTestItemRQ> finishTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); | ||
verify(client, timeout(1000).atLeastOnce()).finishTestItem(same(testMethodUuid), finishTestCaptor.capture()); | ||
|
||
|
||
List<FinishTestItemRQ> finishTests = finishTestCaptor.getAllValues(); | ||
|
||
FinishTestItemRQ testCaseWithException = finishTests.get(0); | ||
|
||
assertThat(testCaseWithException.getDescription(), startsWith(String.format(testErrorMessagePattern, "", testExceptionMessage))); | ||
assertThat(testCaseWithException.getStatus(), equalTo(failedStatus)); | ||
|
||
} | ||
|
||
@Test | ||
public void verify_last_error_log_step() { | ||
ErrorDescriptionTestExtension.init(); | ||
Listener listener = new Listener(); | ||
TestUtils.runClasses(listener, ErrorLastLogFeatureWithStepTest.class); | ||
|
||
ReportPortalClient client = ErrorDescriptionTestExtension.client.get(); | ||
|
||
ArgumentCaptor<FinishTestItemRQ> finishTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); | ||
verify(client, timeout(1000).atLeastOnce()).finishTestItem(same(testMethodUuid), finishTestCaptor.capture()); | ||
|
||
List<FinishTestItemRQ> finishTests = finishTestCaptor.getAllValues(); | ||
|
||
FinishTestItemRQ testCaseWithDescriptionAndStepError = finishTests.get(0); | ||
|
||
assertThat(testCaseWithDescriptionAndStepError.getDescription(), startsWith(String.format(testErrorMessagePattern, stepDescriptionMessage, exceptionStepErrorMessage))); | ||
assertThat(testCaseWithDescriptionAndStepError.getStatus(), equalTo(failedStatus)); | ||
} | ||
|
||
@Test | ||
public void verify_last_error_log_assertion_error() { | ||
ErrorDescriptionTestExtension.init(); | ||
Listener listener = new Listener(); | ||
TestUtils.runClasses(listener, ErrorLastLogFeatureWithAssertionErrorTest.class); | ||
|
||
ReportPortalClient client = ErrorDescriptionTestExtension.client.get(); | ||
|
||
ArgumentCaptor<FinishTestItemRQ> finishTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); | ||
verify(client, timeout(1000).atLeastOnce()).finishTestItem(same(testMethodUuid), finishTestCaptor.capture()); | ||
|
||
List<FinishTestItemRQ> finishTests = finishTestCaptor.getAllValues(); | ||
|
||
FinishTestItemRQ testCaseAssertException = finishTests.get(0); | ||
|
||
assertThat(testCaseAssertException.getDescription(), startsWith(String.format(testErrorMessagePattern, assertDescriptionMessage, assertErrorMessage))); | ||
assertThat(testCaseAssertException.getStatus(), equalTo(failedStatus)); | ||
} | ||
|
||
@Test | ||
public void verify_last_error_log_assertion_passed() { | ||
ErrorDescriptionTestExtension.init(); | ||
Listener listener = new Listener(); | ||
TestUtils.runClasses(listener, ErrorLastLogFeatureWithAssertionPassedTest.class); | ||
|
||
ReportPortalClient client = ErrorDescriptionTestExtension.client.get(); | ||
|
||
ArgumentCaptor<FinishTestItemRQ> finishTestCaptor = ArgumentCaptor.forClass(FinishTestItemRQ.class); | ||
verify(client, timeout(1000).atLeastOnce()).finishTestItem(same(testMethodUuid), finishTestCaptor.capture()); | ||
|
||
List<FinishTestItemRQ> finishTests = finishTestCaptor.getAllValues(); | ||
|
||
FinishTestItemRQ testCaseWithDescriptionAndPassed = finishTests.get(0); | ||
|
||
assertThat(testCaseWithDescriptionAndPassed.getStatus(), equalTo(passedStatus)); | ||
|
||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
.../reportportal/junit5/features/lasterrorlog/ErrorLastLogFeatureWithAssertionErrorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.epam.reportportal.junit5.features.lasterrorlog; | ||
|
||
import com.epam.reportportal.annotations.Description; | ||
import com.epam.reportportal.junit5.ErrorLastLogTest; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
|
||
@ExtendWith(ErrorLastLogTest.ErrorDescriptionTestExtension.class) | ||
public class ErrorLastLogFeatureWithAssertionErrorTest { | ||
|
||
@Test | ||
@Description("0 and 1 is not equal") | ||
public void testWithAssertException() { | ||
Assertions.assertEquals(0, 1); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...reportportal/junit5/features/lasterrorlog/ErrorLastLogFeatureWithAssertionPassedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.epam.reportportal.junit5.features.lasterrorlog; | ||
|
||
import com.epam.reportportal.annotations.Description; | ||
import com.epam.reportportal.annotations.Step; | ||
import com.epam.reportportal.junit5.ErrorLastLogTest; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
@ExtendWith(ErrorLastLogTest.ErrorDescriptionTestExtension.class) | ||
public class ErrorLastLogFeatureWithAssertionPassedTest { | ||
|
||
@Test | ||
@Description("successful test") | ||
public void testWithDescriptionAndPassed() { | ||
login(); | ||
Assertions.assertTrue(true); | ||
} | ||
|
||
@Step | ||
public void login() { | ||
System.out.println("Login successful"); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../epam/reportportal/junit5/features/lasterrorlog/ErrorLastLogFeatureWithExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.epam.reportportal.junit5.features.lasterrorlog; | ||
|
||
import com.epam.reportportal.junit5.ErrorLastLogTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
@ExtendWith(ErrorLastLogTest.ErrorDescriptionTestExtension.class) | ||
public class ErrorLastLogFeatureWithExceptionTest { | ||
|
||
|
||
@Test | ||
public void testWithException() { | ||
throw new RuntimeException("Critical error"); | ||
} | ||
|
||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...a/com/epam/reportportal/junit5/features/lasterrorlog/ErrorLastLogFeatureWithStepTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.epam.reportportal.junit5.features.lasterrorlog; | ||
|
||
import com.epam.reportportal.annotations.Description; | ||
import com.epam.reportportal.annotations.Step; | ||
import com.epam.reportportal.junit5.ErrorLastLogTest; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
@ExtendWith(ErrorLastLogTest.ErrorDescriptionTestExtension.class) | ||
public class ErrorLastLogFeatureWithStepTest { | ||
|
||
@Test | ||
@Description("Login issue") | ||
public void testWithStepError() { | ||
enterCredentials(); | ||
System.out.println("Username is not correct"); | ||
loginWithException(); | ||
Assertions.assertTrue(Boolean.TRUE); | ||
} | ||
|
||
@Step | ||
@Description("Credentials entered") | ||
public void enterCredentials() { | ||
Assertions.assertTrue(Boolean.TRUE); | ||
} | ||
@Step | ||
public void loginWithException() { | ||
throw new NoSuchElementException("Error message"); | ||
} | ||
} |