How to use JUnitSetupPrinter class

Best Behat code snippet using .JUnitSetupPrinter

Run Behat automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

JUnitSetupPrinter.php

Source: JUnitSetupPrinter.php Github

copy
1<?php
2namespace Behat\Behat\Output\Node\Printer\JUnit;
3
4use Behat\Behat\Hook\Scope\StepScope;
5use Behat\Behat\Output\Node\Printer\SetupPrinter;
6use Behat\Testwork\Call\CallResult;
7use Behat\Testwork\Call\CallResults;
8use Behat\Testwork\Exception\ExceptionPresenter;
9use Behat\Testwork\Hook\Call\HookCall;
10use Behat\Testwork\Hook\Tester\Setup\HookedSetup;
11use Behat\Testwork\Hook\Tester\Setup\HookedTeardown;
12use Behat\Testwork\Output\Formatter;
13use Behat\Testwork\Output\Printer\JUnitOutputPrinter;
14use Behat\Testwork\Tester\Setup\Setup;
15use Behat\Testwork\Tester\Setup\Teardown;
16
17/**
18 * @author: Jakob Erdmann <jakob.erdmann@rocket-internet.com>
19 */
20class JUnitSetupPrinter implements SetupPrinter
21{
22
23    /** @var ExceptionPresenter */
24    private $exceptionPresenter;
25
26    public function __construct(ExceptionPresenter $exceptionPresenter)
27    {
28        $this->exceptionPresenter = $exceptionPresenter;
29    }
30
31    /**
32     * {@inheritdoc}
33     */
34    public function printSetup(Formatter $formatter, Setup $setup)
35    {
36        if (!$setup->isSuccessful()) {
37            if ($setup instanceof HookedSetup) {
38                $this->handleHookCalls($formatter, $setup->getHookCallResults(), 'setup');
39            }
40        }
41    }
42
43    /**
44     * {@inheritdoc}
45     */
46    public function printTeardown(Formatter $formatter, Teardown $teardown)
47    {
48        if (!$teardown->isSuccessful()) {
49            if ($teardown instanceof HookedTeardown) {
50                $this->handleHookCalls($formatter, $teardown->getHookCallResults(), 'teardown');
51            }
52        }
53    }
54
55    /**
56     * @param Formatter $formatter
57     * @param CallResults $results
58     * @param string $messageType
59     */
60    private function handleHookCalls(Formatter $formatter, CallResults $results, $messageType)
61    {
62        /** @var CallResult $hookCallResult */
63        foreach ($results as $hookCallResult) {
64            if ($hookCallResult->hasException()) {
65                /** @var HookCall $call */
66                $call = $hookCallResult->getCall();
67                $scope = $call->getScope();
68                /** @var JUnitOutputPrinter $outputPrinter */
69                $outputPrinter = $formatter->getOutputPrinter();
70
71                $message = '';
72                if ($scope instanceof StepScope) {
73                    $message .= $scope->getStep()->getKeyword() . ' ' . $scope->getStep()->getText() . ': ';
74                }
75                $message .= $this->exceptionPresenter->presentException($hookCallResult->getException());
76
77                $attributes = array(
78                    'message' => $message,
79                    'type'    => $messageType,
80                );
81
82                $outputPrinter->addTestcaseChild('failure', $attributes);
83
84            }
85        }
86    }
87}
88
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in JUnitSetupPrinter

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)