From f4230362be034a14931f0ae24c0b1388c33f2728 Mon Sep 17 00:00:00 2001 From: Pheodor Paramonov Date: Sat, 24 Nov 2018 18:22:24 -0600 Subject: [PATCH] [#8] Fixed usage of ExecutionResult in result printers --- .../lifecycle/TestCaseIlluminator.java | 2 +- .../lifecycle/TestCaseSummaryPrinter.java | 21 +++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/sprimber-engine/src/main/java/com/griddynamics/qa/sprimber/lifecycle/TestCaseIlluminator.java b/sprimber-engine/src/main/java/com/griddynamics/qa/sprimber/lifecycle/TestCaseIlluminator.java index f67fc74..7cd420e 100644 --- a/sprimber-engine/src/main/java/com/griddynamics/qa/sprimber/lifecycle/TestCaseIlluminator.java +++ b/sprimber-engine/src/main/java/com/griddynamics/qa/sprimber/lifecycle/TestCaseIlluminator.java @@ -75,7 +75,7 @@ public void illuminateTestCaseStart(TestCaseStartedEvent startEvent) { @EventListener public void illuminateTestCaseFinish(TestCaseFinishedEvent finishEvent) { LOGGER.debug("Test case finished: '{}' with status {}", - finishEvent.getTestCase().getName(), finishEvent.getExecutionResult()); + finishEvent.getTestCase().getName(), finishEvent.getExecutionResult().getStatus()); } @EventListener diff --git a/sprimber-engine/src/main/java/com/griddynamics/qa/sprimber/lifecycle/TestCaseSummaryPrinter.java b/sprimber-engine/src/main/java/com/griddynamics/qa/sprimber/lifecycle/TestCaseSummaryPrinter.java index 119d082..6b3f977 100644 --- a/sprimber-engine/src/main/java/com/griddynamics/qa/sprimber/lifecycle/TestCaseSummaryPrinter.java +++ b/sprimber-engine/src/main/java/com/griddynamics/qa/sprimber/lifecycle/TestCaseSummaryPrinter.java @@ -24,6 +24,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE package com.griddynamics.qa.sprimber.lifecycle; +import com.griddynamics.qa.sprimber.engine.model.ExecutionResult; import com.griddynamics.qa.sprimber.lifecycle.model.executor.testcase.TestCaseFinishedEvent; import com.griddynamics.qa.sprimber.lifecycle.model.executor.testcase.TestCaseStartedEvent; import com.griddynamics.qa.sprimber.lifecycle.model.executor.testhook.TestHookFinishedEvent; @@ -34,9 +35,10 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE import org.springframework.core.NamedThreadLocal; import java.util.Arrays; +import java.util.Optional; /** - * This is a helper class that able to print shor summary after each scenario to info log level + * This is a helper class that able to print short summary after each scenario to info log level * By default this bean not configured automatically, * use property {@code sprimber.configuration.summary.printer.enable} to enable it * @@ -46,13 +48,14 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE public class TestCaseSummaryPrinter { private static final Logger LOGGER = LoggerFactory.getLogger(TestCaseSummaryPrinter.class); + private static final String EMPTY_STRING = ""; private ThreadLocal reportBuilder = new NamedThreadLocal<>("Testcase report builder"); @EventListener public void illuminateTestCaseStart(TestCaseStartedEvent startEvent) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("\n\n"); - stringBuilder.append(String.format("Test Case Started: %s", startEvent.getTestCase().getName())); + stringBuilder.append(String.format("Test Case Completed: %s", startEvent.getTestCase().getName())); reportBuilder.set(stringBuilder); } @@ -71,14 +74,24 @@ public void illuminateTestStepFinish(TestStepFinishedEvent finishEvent) { stringBuilder.append(String.format(" %s", finishEvent.getTestStep().getStepAction().getActionType())); stringBuilder.append(String.format(" %s", finishEvent.getTestStep().getActualText())); stringBuilder.append(String.format(" %s", Arrays.toString(finishEvent.getTestStep().getStepArguments()))); - stringBuilder.append(String.format(" (%s) ", finishEvent.getExecutionResult())); + stringBuilder.append(String.format(" (%s) ", finishEvent.getExecutionResult().getStatus())); + stringBuilder.append(String.format(" (%s) ", getExceptionMessageIfAny(finishEvent.getExecutionResult()))); } @EventListener public void illuminateTestHookFinish(TestHookFinishedEvent finishEvent) { StringBuilder stringBuilder = reportBuilder.get(); stringBuilder.append("\n"); - stringBuilder.append(String.format(" %s from:", finishEvent.getHookDefinition().getActionType())); + stringBuilder.append(String.format(" %s of scope", finishEvent.getHookDefinition().getActionType())); + stringBuilder.append(String.format(" %s from:", finishEvent.getHookDefinition().getActionScope())); stringBuilder.append(String.format(" %s", finishEvent.getHookDefinition().getMethod())); } + + private String getExceptionMessageIfAny(ExecutionResult executionResult) { + return executionResult.getOptionalError().map(this::buildExceptionMessage).orElse(EMPTY_STRING); + } + + private String buildExceptionMessage(Throwable throwable) { + return Optional.ofNullable(throwable.getMessage()).orElse(throwable.getClass().getName()); + } }