yoanm / behat-utils-extension
A set of utility for Behat3
Installs: 26 951
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: >=5.5
- behat/behat: ~3.0
- monolog/monolog: ~1.0
Requires (Dev)
Suggests
- yoanm/behat3-symfony-extension: Symfony integration for behat v3.0
README
BehatUtilsExtension provide a set of utility for Behat 3.0+
Install
composer require --dev yoanm/behat-utils-extension
How to use
BehatUtilsExtension require behat/behat and monolog/monolog
Configuration
Add the following in your behat configuration file (usually behat.yml
) :
default: extensions: Yoanm\BehatUtilsExtension: ~
In the box
Logger
Implements this interface and your context will have a logger injected
Example
<?php namespace Functional\Yoanm\BehatUtilsExtension\Context; use Behat\Behat\Context\Context; use Psr\Log\LoggerInterface; use Yoanm\BehatUtilsExtension\Context\LoggerAwareInterface; class FeatureContext implements Context, LoggerAwareInterface { /** @var LoggerInterface */ private $logger; /** * {@inheritdoc} */ public function setBehatLogger(LoggerInterface $logger) { $this->logger = $logger; } /** * @When my step */ public function myStep() { $this->logger->info('Executing my step'); } }
Behat event subscription
See BehatContextSubscriberInterface
Enable initializer and implements BehatContextSubscriberInterface
, then your context will be passed to Behat dispatcher in order to receive behat events
Enable context initializer
default: extensions: Yoanm\BehatUtilsExtension: event_subscriber: true
Example
<?php namespace Functional\Yoanm\BehatUtilsExtension\Context; use Behat\Behat\Context\Context; use Behat\Behat\EventDispatcher\Event\ExampleTested; use Behat\Behat\EventDispatcher\Event\GherkinNodeTested; use Behat\Behat\EventDispatcher\Event\ScenarioTested; use Yoanm\BehatUtilsExtension\Context\BehatContextSubscriberInterface; class FeatureContext implements Context, BehatContextSubscriberInterface { /** * @param GherkinNodeTested $event */ public function reset(GherkinNodeTested $event) { /** * Reset here your data before a scenario or example is started */ } /** * {@inheritdoc} */ public static function getSubscribedEvents() { return [ ScenarioTested::BEFORE => ['reset'], ExampleTested::BEFORE => ['reset'], //Or // ScenarioTested::BEFORE => ['reset', ListenerPriority::HIGH_PRIORITY], ]; } }
Step logger
If enabled, will log each behat feature/background/outline/example/step start and end. Usefull to understand what happens behing the scene if you have a non understandable error in you features.
Could be use with Logger to easily spot an issue in context custom steps.
Enable step listener
default: extensions: Yoanm\BehatUtilsExtension: step_logger: true
Output example
The following will be appended in the configured log file (see default configuration reference below).
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [FEATURE][IN] {"title":"FEATURE TITLE","file":"FEATURE FILE PATH"} []
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [FEATURE][OUT] {"title":"FEATURE TITLE","file":"FEATURE FILE PATH"} []
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [BACKGROUND][IN] {"title":"BACKGROUND TITLE","line":"BACKGROUND START LINE"} []
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [BACKGROUND][OUT] {"title":"BACKGROUND TITLE","line":"BACKGROUND END LINE"} []
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [OUTLINE][IN] {"title":"OUTLINE TITLE","line": "OUTLINE START LINE"} []
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [OUTLINE][OUT] {"title":"OUTLINE TITLE","line": "OUTLINE END LINE"} []
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [EXAMPLE][IN] {"tokens":{"EXAMPLE_TOKENS_NAME":"EXAMPLE_TOKENS_VALUE"},"title":"| EXAMPLE_TOKENS_VALUE|","line":"EXAMPLE START LINE"} []
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [EXAMPLE][OUT] {"tokens":{"EXAMPLE_TOKENS_NAME":"EXAMPLE_TOKENS_VALUE"},"title":"| EXAMPLE_TOKENS_VALUE|","line":"EXAMPLE END LINE"} []
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [STEP][IN] {"text":"STEP TEXT","line":"STEP LINE"} []
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [STEP][OUT] {"text":"STEP TEXT","line":"STEP LINE"} []
Configuration reference
default: extensions: Yoanm\BehatUtilsExtension: logger: path: behat.log level: INFO event_subscriber: false step_logger: false
Tests
This repository follow a custom test strategy