/** * Adds step with provided name and status in current test or step (or test fixture). Takes no effect * if no test run at the moment. * * @param name the name of step. * @param status the step status. */ public static void step(final String name, final Status status) { final String uuid = UUID.randomUUID().toString(); getLifecycle().startStep(uuid, new StepResult().setName(name).setStatus(status)); getLifecycle().stopStep(uuid); }
@Override public void testStarted(final Description description) { final String uuid = testCases.get(); final TestResult result = createTestResult(uuid, description); getLifecycle().scheduleTestCase(result); getLifecycle().startTestCase(uuid); }
@AfterReturning(pointcut = "anyMethod() && withStepAnnotation()") public void stepStop() { getLifecycle().updateStep(s -> s.setStatus(Status.PASSED)); getLifecycle().stopStep(); }
/** * Adds attachment to current running test or step. * * @param name the name of attachment * @param type the content type of attachment * @param fileExtension the attachment file extension * @param stream attachment content */ public void addAttachment(final String name, final String type, final String fileExtension, final InputStream stream) { writeAttachment(prepareAttachment(name, type, fileExtension), stream); }
@Override public void endOfScenarioLifeCycle(final Scenario scenario) { final StepUtils stepUtils = new StepUtils(currentFeature, currentScenario); synchronized (gherkinSteps) { while (gherkinSteps.peek() != null) { stepUtils.fireCanceledStep(gherkinSteps.remove()); } } final String scenarioUuid = scenarioUuids.remove(scenario); lifecycle.stopTestCase(scenarioUuid); lifecycle.writeTestCase(scenarioUuid); }
final Status status = ResultsUtils.getStatus(result.getError()) .orElse(Status.FAILED); lifecycle.updateStep(stepResult -> stepResult.setStatus(Status.FAILED)); lifecycle.updateTestCase(scenarioUuid, scenarioResult -> scenarioResult.setStatus(status) .setStatusDetails(statusDetails)); lifecycle.stopStep(); break; case PENDING: lifecycle.updateStep(stepResult -> stepResult.setStatus(Status.SKIPPED)); lifecycle.updateTestCase(scenarioUuid, scenarioResult -> scenarioResult.setStatus(Status.SKIPPED) .setStatusDetails(statusDetails)); lifecycle.stopStep(); break; case SKIPPED: lifecycle.updateStep(stepResult -> stepResult.setStatus(Status.SKIPPED)); lifecycle.stopStep(); break; case PASSED: lifecycle.updateStep(stepResult -> stepResult.setStatus(Status.PASSED)); lifecycle.stopStep(); lifecycle.updateTestCase(scenarioUuid, scenarioResult -> scenarioResult.setStatus(Status.PASSED) .setStatusDetails(statusDetails));
@Override public void onEvent(final LogEvent event) { lifecycle.getCurrentTestCase().ifPresent(uuid -> { final String stepUUID = UUID.randomUUID().toString(); lifecycle.startStep(stepUUID, new StepResult() .setName(event.toString()) .setStatus(Status.PASSED)); lifecycle.updateStep(stepResult -> stepResult.setStart(stepResult.getStart() - event.getDuration())); if (LogEvent.EventStatus.FAIL.equals(event.getStatus())) { if (saveScreenshots) { lifecycle.addAttachment("Screenshot", "image/png", "png", getScreenshotBytes()); } if (savePageHtml) { lifecycle.addAttachment("Page source", "text/html", "html", getPageSourceBytes()); } lifecycle.updateStep(stepResult -> { final StatusDetails details = ResultsUtils.getStatusDetails(event.getError()) .orElse(new StatusDetails()); stepResult.setStatus(ResultsUtils.getStatus(event.getError()).orElse(Status.BROKEN)); stepResult.setStatusDetails(details); }); } lifecycle.stopStep(stepUUID); }); }
protected void fireFixtureStep(final Match match, final Result result, final boolean isBefore) { final String uuid = md5(match.getLocation()); final StepResult stepResult = new StepResult() .setName(match.getLocation()) .setStatus(Status.fromValue(result.getStatus())) .setStart(System.currentTimeMillis() - result.getDuration()) .setStop(System.currentTimeMillis()); if (FAILED.equals(result.getStatus())) { final StatusDetails statusDetails = ResultsUtils.getStatusDetails(result.getError()).get(); stepResult.setStatusDetails(statusDetails); if (isBefore) { final TagParser tagParser = new TagParser(feature, scenario); statusDetails .setMessage("Before is failed: " + result.getError().getLocalizedMessage()) .setFlaky(tagParser.isFlaky()) .setMuted(tagParser.isMuted()) .setKnown(tagParser.isKnown()); lifecycle.updateTestCase(scenario.getId(), scenarioResult -> scenarioResult.setStatus(Status.SKIPPED) .setStatusDetails(statusDetails)); } } lifecycle.startStep(scenario.getId(), uuid, stepResult); lifecycle.stopStep(uuid); } }
@Override public void testFinished(final Description description) { final String uuid = testCases.get(); testCases.remove(); getLifecycle().updateTestCase(uuid, testResult -> { if (Objects.isNull(testResult.getStatus())) { testResult.setStatus(Status.PASSED); } }); getLifecycle().stopTestCase(uuid); getLifecycle().writeTestCase(uuid); }
/** * Run provided {@link ThrowableRunnable} as step with given name. Takes no effect * if no test run at the moment. * * @param runnable the step's body. */ public static <T> T step(final ThrowableContextRunnable<T, StepContext> runnable) { final String uuid = UUID.randomUUID().toString(); getLifecycle().startStep(uuid, new StepResult().setName("step")); try { final T result = runnable.run(new DefaultStepContext(uuid)); getLifecycle().updateStep(uuid, step -> step.setStatus(Status.PASSED)); return result; } catch (Throwable throwable) { getLifecycle().updateStep(s -> s .setStatus(getStatus(throwable).orElse(Status.BROKEN)) .setStatusDetails(getStatusDetails(throwable).orElse(null))); ExceptionUtils.sneakyThrow(throwable); return null; } finally { getLifecycle().stopStep(uuid); } }
/** * Start a new step as child step of current running test case or step. Shortcut * for {@link #startStep(String, String, StepResult)}. * * @param uuid the uuid of step. * @param result the step. */ public void startStep(final String uuid, final StepResult result) { final Optional<String> current = threadContext.getCurrent(); if (!current.isPresent()) { LOGGER.error("Could not start step: no test case running"); return; } final String parentUuid = current.get(); startStep(parentUuid, uuid, result); }
@Override public void testIgnored(final Description description) { final String uuid = testCases.get(); testCases.remove(); final TestResult result = createTestResult(uuid, description); result.setStatus(Status.SKIPPED); result.setStatusDetails(getIgnoredMessage(description)); result.setStart(System.currentTimeMillis()); getLifecycle().scheduleTestCase(result); getLifecycle().stopTestCase(uuid); getLifecycle().writeTestCase(uuid); }
/** * Shortcut for {@link #updateTestCase(String, Consumer)} for current running test case uuid. * * @param update the update function. */ public void updateTestCase(final Consumer<TestResult> update) { final Optional<String> root = threadContext.getRoot(); if (!root.isPresent()) { LOGGER.error("Could not update test case: no test case running"); return; } final String uuid = root.get(); updateTestCase(uuid, update); }
public static void stepFinish() { getLifecycle().stopStep(STEP_UUID_STACK.get().removeFirst()); } }
/** * Updates current step. Shortcut for {@link #updateStep(String, Consumer)}. * * @param update the update function. */ public void updateStep(final Consumer<StepResult> update) { final Optional<String> current = threadContext.getCurrent(); if (!current.isPresent()) { LOGGER.error("Could not update step: no step running"); return; } final String uuid = current.get(); updateStep(uuid, update); }
/** * Schedules test case with given parent. * * @param containerUuid the uuid of container. * @param result the test case to schedule. */ public void scheduleTestCase(final String containerUuid, final TestResult result) { storage.getContainer(containerUuid).ifPresent(container -> { synchronized (storage) { container.getChildren().add(result.getUuid()); } }); scheduleTestCase(result); }
private void handlePickleStep(final TestStepFinished event) { final StatusDetails statusDetails; if (event.result.getStatus() == Result.Type.UNDEFINED) { statusDetails = getStatusDetails(new PendingException("TODO: implement me")) .orElse(new StatusDetails()); lifecycle.updateTestCase(getTestCaseUuid(currentTestCase), scenarioResult -> scenarioResult .setStatus(translateTestCaseStatus(event.result)) .setStatusDetails(statusDetails)); } else { statusDetails = getStatusDetails(event.result.getError()) .orElse(new StatusDetails()); } final TagParser tagParser = new TagParser(currentFeature, currentTestCase); statusDetails .setFlaky(tagParser.isFlaky()) .setMuted(tagParser.isMuted()) .setKnown(tagParser.isKnown()); lifecycle.updateStep(getStepUuid(event.testStep), stepResult -> stepResult.setStatus(translateTestCaseStatus(event.result))); lifecycle.stopStep(getStepUuid(event.testStep)); } }
protected void fireCanceledStep(final Step unimplementedStep) { final StepResult stepResult = new StepResult(); stepResult.setName(unimplementedStep.getName()) .setStart(System.currentTimeMillis()) .setStop(System.currentTimeMillis()) .setStatus(Status.SKIPPED) .setStatusDetails(new StatusDetails().setMessage("Unimplemented step")); lifecycle.startStep(scenario.getId(), getStepUuid(unimplementedStep), stepResult); lifecycle.stopStep(getStepUuid(unimplementedStep)); final StatusDetails statusDetails = new StatusDetails(); final TagParser tagParser = new TagParser(feature, scenario); statusDetails .setFlaky(tagParser.isFlaky()) .setMuted(tagParser.isMuted()) .setKnown(tagParser.isKnown()); lifecycle.updateTestCase(scenario.getId(), scenarioResult -> scenarioResult.setStatus(Status.SKIPPED) .setStatusDetails(statusDetails .setMessage("Unimplemented steps were found"))); }
private void handleTestCaseFinished(final TestCaseFinished event) { final String uuid = getTestCaseUuid(event.testCase); lifecycle.updateTestCase( uuid, testResult -> testResult.setStatus(translateTestCaseStatus(event.result)) ); final Optional<StatusDetails> details = getStatusDetails(event.result.getError()); details.ifPresent(statusDetails -> lifecycle.updateTestCase( uuid, testResult -> testResult.setStatusDetails(statusDetails) )); lifecycle.stopTestCase(uuid); lifecycle.writeTestCase(uuid); }
public static CompletableFuture<InputStream> addStreamAttachmentAsync( final String name, final String type, final String fileExtension, final Supplier<InputStream> body) { final String source = lifecycle.prepareAttachment(name, type, fileExtension); return supplyAsync(body).whenComplete((result, ex) -> lifecycle.writeAttachment(source, result)); }