Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log an exception if it happened not just in a step but in a test method as well #90

Merged
merged 1 commit into from
Mar 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/com/github/invictum/reportportal/Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.invictum.reportportal;

import net.thucydides.core.model.TestOutcome;
import net.thucydides.core.model.TestResult;
import net.thucydides.core.model.TestStep;

Expand Down Expand Up @@ -30,6 +31,17 @@ public static Date stepEndDate(TestStep step) {
return Date.from(endTimeZoned.toInstant());
}

/**
* Calculates test's end time.
*
* @param test to calculate time on
* @return test end time in {@link Date} format
*/
public static Date testEndDate(TestOutcome test) {
ZonedDateTime endTimeZoned = test.getStartTime().plus(Duration.ofMillis(test.getDuration()));
return Date.from(endTimeZoned.toInstant());
}

/**
* Calculates step's start time
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.epam.ta.reportportal.ws.model.log.SaveLogRQ;
import com.github.invictum.reportportal.Utils;

import net.thucydides.core.model.TestOutcome;
import net.thucydides.core.model.TestStep;

import java.io.PrintWriter;
Expand All @@ -12,17 +14,26 @@

public class Error {

private static final Function<TestStep, String> DEFAULT_FORMATTER = step -> {
private static final Function<TestStep, String> STEP_FORMATTER = step -> {
Throwable cause = step.getException().getOriginalCause();
return stringifyStackTrace(cause);
};

private static final Function<TestOutcome, String> TEST_FORMATTER = testOutcome -> {
Throwable cause = testOutcome.getTestFailureCause().toException();
return stringifyStackTrace(cause);
};

private static String stringifyStackTrace(Throwable cause){
StringWriter writer = new StringWriter();
cause.printStackTrace(new PrintWriter(writer));
return writer.toString();
};
}

/**
* Logs error using preconfigured {@link Function} representation of formatter
*/
public static Function<TestStep, Collection<SaveLogRQ>> configuredError(Function<TestStep, String> errorFormatter) {
public static Function<TestStep, Collection<SaveLogRQ>> configuredStepError(Function<TestStep, String> errorFormatter) {
return step -> {
if (step.getException() != null) {
SaveLogRQ log = new SaveLogRQ();
Expand All @@ -36,9 +47,32 @@ public static Function<TestStep, Collection<SaveLogRQ>> configuredError(Function
}

/**
* Logs error using default error formatter - print error with full stack trace
* Logs error using preconfigured {@link Function} representation of formatter
*/
public static Function<TestOutcome, Collection<SaveLogRQ>> configuredTestError(Function<TestOutcome, String> errorFormatter) {
return testOutcome -> {
if (!testOutcome.getFailingStep().isPresent() && testOutcome.getTestFailureCause() != null) {
SaveLogRQ log = new SaveLogRQ();
log.setMessage(errorFormatter.apply(testOutcome));
log.setLevel(Utils.logLevel(testOutcome.getResult()));
log.setLogTime(Utils.testEndDate(testOutcome));
return Collections.singleton(log);
}
return Collections.emptySet();
};
}

/**
* Logs error in step using default error formatter - print error with full stack trace
*/
public static Function<TestStep, Collection<SaveLogRQ>> basic() {
return configuredError(DEFAULT_FORMATTER);
return configuredStepError(STEP_FORMATTER);
}

/**
* Logs error in test (outside of a step) using default error formatter - print error with full stack trace
*/
public static Function<TestOutcome, Collection<SaveLogRQ>> errorInTest() {
return configuredTestError(TEST_FORMATTER);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package com.github.invictum.reportportal.recorder;

import java.util.Collection;

import net.thucydides.core.model.TestOutcome;

import com.epam.reportportal.service.Launch;
import com.epam.reportportal.service.ReportPortal;
import com.epam.ta.reportportal.ws.model.FinishTestItemRQ;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
import com.github.invictum.reportportal.*;
import com.epam.ta.reportportal.ws.model.log.SaveLogRQ;
import com.github.invictum.reportportal.FinishEventBuilder;
import com.github.invictum.reportportal.ItemType;
import com.github.invictum.reportportal.LogUnitsHolder;
import com.github.invictum.reportportal.StartEventBuilder;
import com.github.invictum.reportportal.Status;
import com.github.invictum.reportportal.SuiteStorage;
import com.github.invictum.reportportal.log.unit.Error;
import com.google.inject.Inject;
import io.reactivex.Maybe;
import net.thucydides.core.model.TestOutcome;

/**
* Common test recorder suitable for most cases
Expand Down Expand Up @@ -40,6 +51,8 @@ public void record(TestOutcome out) {
Maybe<String> testId = launch.startTestItem(id, builder.build());
// Steps
out.getFlattenedTestSteps().forEach(holder::proceed);
// failed assertions in test itself
recordNonStepFailure(out);
FinishTestItemRQ finishTest = new FinishEventBuilder()
.withStatus(Status.mapTo(out.getResult()))
.withEndTime(out.getStartTime(), out.getDuration())
Expand All @@ -51,4 +64,12 @@ public void record(TestOutcome out) {
.build();
suiteStorage.suiteFinisher(out.getUserStory().getId(), () -> launch.finishTestItem(id, finishSuite));
}

private void recordNonStepFailure(TestOutcome out){
Collection<SaveLogRQ> logs = Error.errorInTest().apply(out);
logs.forEach(l -> ReportPortal.emitLog(id -> {
l.setTestItemId(id);
return l;
}));
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
package com.github.invictum.reportportal.log.unit;

import com.epam.ta.reportportal.ws.model.log.SaveLogRQ;
import com.github.invictum.reportportal.LogLevel;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Iterator;
import java.util.Optional;

import net.thucydides.core.model.TestOutcome;
import net.thucydides.core.model.TestResult;
import net.thucydides.core.model.TestStep;
import net.thucydides.core.model.stacktrace.FailureCause;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import java.time.ZonedDateTime;
import java.util.Collection;
import com.epam.ta.reportportal.ws.model.log.SaveLogRQ;
import com.github.invictum.reportportal.LogLevel;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ErrorTest {

@Mock
private TestStep stepMock;

@Mock(lenient = true)
private TestOutcome testOutcomeMock;

@Mock
private FailureCause failureCauseMock;

Expand All @@ -36,12 +44,29 @@ public void customErrorProcessing() {
Mockito.when(stepMock.getStartTime()).thenReturn(ZonedDateTime.now());
Mockito.when(stepMock.getException()).thenReturn(failureCauseMock);
Mockito.when(stepMock.getConciseErrorMessage()).thenReturn("Custom error");
SaveLogRQ actual = Error.configuredError(TestStep::getConciseErrorMessage).apply(stepMock).iterator().next();
SaveLogRQ actual = Error.configuredStepError(TestStep::getConciseErrorMessage).apply(stepMock).iterator().next();
// Verification
Assert.assertEquals("Custom error", actual.getMessage());
Assert.assertEquals(LogLevel.ERROR.toString(), actual.getLevel());
}

@Test
public void errorAtTestLevelShouldBeLogged(){
Mockito.when(testOutcomeMock.getStartTime()).thenReturn(ZonedDateTime.now());
Mockito.when(testOutcomeMock.getTestFailureCause()).thenReturn(new FailureCause(new RuntimeException()));
Iterator<SaveLogRQ> iterator = Error.configuredTestError(TestOutcome::getConciseErrorMessage).apply(testOutcomeMock).iterator();
Assert.assertTrue(iterator.hasNext());
}

@Test
public void errorAtStepLevelShouldNotBeLoggedAtTestLevel(){
Mockito.when(testOutcomeMock.getStartTime()).thenReturn(ZonedDateTime.now());
Mockito.when(testOutcomeMock.getTestFailureCause()).thenReturn(new FailureCause(new RuntimeException()));
Mockito.when(testOutcomeMock.getFailingStep()).thenReturn(Optional.of(new TestStep()));
Iterator<SaveLogRQ> iterator = Error.configuredTestError(TestOutcome::getConciseErrorMessage).apply(testOutcomeMock).iterator();
Assert.assertFalse(iterator.hasNext());
}

@Test
public void defaultError() {
// Setup mock
Expand Down