Skip to content

Instantly share code, notes, and snippets.

@Quilted
Last active August 29, 2015 14:03
Show Gist options
  • Save Quilted/33deadac448638560ed1 to your computer and use it in GitHub Desktop.
Save Quilted/33deadac448638560ed1 to your computer and use it in GitHub Desktop.
Behat: Add a terse formatter

Overview

These files set up another formatter, like 'html', 'pretty', or 'progress', called terse. The terse formatter prints only feature names and scenario names. It does not print tags or steps.

Usage

Use it like any other Behat formatter. For example: bin/behat --format terse This formatter is particularly useful in combination with the --dry-run flag.

Install

  1. Add TerseFormatter.php to vendor/behat/behat/src/Behat/Behat/Formatter/.
  2. Add the behat.xml snippet to vendor/behat/behat/src/Behat/Behat/DependencyInjection/config/behat.xml in the section with the other formatters.
<service id="behat.formatter.dispatcher.terse" class="%behat.formatter.dispatcher.class%">
<argument>Behat\Behat\Formatter\TerseFormatter</argument>
<argument>terse</argument>
<argument>Prints scenarios only.</argument>
<tag name="behat.formatter.dispatcher" />
</service>
<?php
namespace Behat\Behat\Formatter;
use Behat\Gherkin\Node\FeatureNode;
use Behat\Gherkin\Node\AbstractScenarioNode;
use Behat\Behat\Event\BackgroundEvent;
use Behat\Gherkin\Node\ScenarioNode;
class TerseFormatter extends PrettyFormatter
{
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
$events = array(
// 'beforeSuite', 'afterSuite', 'beforeFeature', 'afterFeature', 'beforeScenario',
// 'afterScenario', 'beforeBackground', 'afterBackground', 'beforeOutline', 'afterOutline',
// 'beforeOutlineExample', 'afterOutlineExample', 'afterStep'
'beforeSuite', 'afterSuite', 'beforeFeature', 'afterFeature', 'beforeScenario',
'afterScenario', 'beforeBackground', 'afterBackground', 'beforeOutline', 'afterOutline',
'beforeOutlineExample', 'afterOutlineExample'
);
return array_combine($events, $events);
}
/**
* Listens to "background.before" event.
*
* @param BackgroundEvent $event
*
* @uses printBackgroundHeader()
*/
public function beforeBackground(BackgroundEvent $event)
{
$this->inBackground = true;
if ($this->isBackgroundPrinted) {
return;
}
// $this->printBackgroundHeader($event->getBackground());
}
/**
* Listens to "background.after" event.
*
* @param BackgroundEvent $event
*
* @uses printBackgroundFooter()
*/
public function afterBackground(BackgroundEvent $event)
{
$this->inBackground = false;
if ($this->isBackgroundPrinted) {
return;
}
$this->isBackgroundPrinted = true;
// $this->printBackgroundFooter($event->getBackground());
if (null !== $this->delayedScenarioEvent) {
$method = $this->delayedScenarioEvent[0];
$event = $this->delayedScenarioEvent[1];
$this->$method($event);
}
}
/**
* Prints feature header.
*
* @param FeatureNode $feature
*
* @uses printFeatureOrScenarioTags()
* @uses printFeatureName()
* @uses printFeatureDescription()
*/
protected function printFeatureHeader(FeatureNode $feature)
{
// $this->printFeatureOrScenarioTags($feature);
$this->printFeatureName($feature);
// if (null !== $feature->getDescription()) {
// $this->printFeatureDescription($feature);
// }
$this->writeln();
}
/**
* Prints feature footer.
*
* @param FeatureNode $feature
*/
protected function printFeatureFooter(FeatureNode $feature)
{
$this->writeln();
}
/**
* Prints scenario header.
*
* @param ScenarioNode $scenario
*
* @uses printFeatureOrScenarioTags()
* @uses printScenarioName()
*/
protected function printScenarioHeader(ScenarioNode $scenario)
{
$this->maxLineLength = $this->getMaxLineLength($this->maxLineLength, $scenario);
// $this->printFeatureOrScenarioTags($scenario);
$this->printScenarioName($scenario);
}
/**
* Prints scenario keyword and name.
*
* @param AbstractScenarioNode $scenario
*
* @uses getFeatureOrScenarioName()
* @uses printScenarioPath()
*/
protected function printScenarioName(AbstractScenarioNode $scenario)
{
$title = explode("\n", $this->getFeatureOrScenarioName($scenario));
$this->write(array_shift($title));
// $this->printScenarioPath($scenario);
if (count($title)) {
$this->writeln(implode("\n", $title));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment