Better, faster, stronger
behat steps with
ScenarioStateBehatExtension
Behavior Driven Development
GOAL: Validate the
features/contracts, not the
implementation details
(a practical definition)
Unit tests
e2e
Acceptance
Integration tests
Somw’here
around there
From API level to GUI!
Gherkin : A Domain Specific Language to the rescue
FEATURE
Description of what should beexpected from a specific aspectof the system.
GIVEN
The system under test is
setup to match a specific and
benevolent context where the
test can be confidently
executed.
SCENARIO
A sequence of    stepsthat verify t  the
correct           behavior of the       system.
WHEN
The system under test ischallenged via its exposedpossible interactions (API,GUI, etc.).
THEN
The test runner analyze
the system under test’s
reaction and verify some
assertions.
The PHP
reference
gherkin test runner!
A Step implementation with BEHAT
State management : Where does it belong?
When the system under
test is stateful
(Like most Single Page Apps or Good’ol server
side session_start() websites)
State is chiefly handled by the system itself.
By running the steps in sequence we can
safely assume that the context in which our
steps are executed is correct and consistent.
This is the kind of environments
documentations and blogs usually use to
illustrate what they have to say.
The easy part :
Stateless Systems
(API)
By definition each interaction
with the system is one shot and
the system keeps absolutely no
track of what is going on.
Therefore, state management
must be done by the client, ie.
our tests implementations.
The not so easy:
Out of the box
solution: CONTEXTS
Behat contexts are destroyed
and recreated between each
scenario.
Then, simply use:
$this->myStateFragment = $value;
may very well be enough in most
cases.
The not so easy:But…
…sharing state fragments amongst multiple
contexts quickly becomes painful:
Strong coupling (it’s bad) is induced between
contexts (this one can be solved by writing a
specific context whose responsibility is solely
storing state fragments).
The dependency to a state fragment by a step
is hidden in the step implementation and can
not be deduced from step params.
The state management logic is dispatched in
steps implementation, it adds unnecessary
visual debt and dilute the core of the step.
OH!
The BEHAT
reference
STATE M
ANAGEM
ENT
HANDLER!
@vincentchalamon
@gorghoa
Features:
● Share state fragments with all steps
for a one (and only one) scenario;
● Inject state fragment directly to steps
functions as arguments;
● Fails fast if context is unstable
(missing state fragment dependency
for a step);
● Reduce the steps code to the bare
necessities;
● Leverage PHP type hints;
gorghoa/ScenarioStateBehatExtension
Set up : Flag a Context as state management aware
Share a state fragment
Consume a state fragment
And yeah, that is all, folks:
A simple yet effective way to
manage scenario states, letting
you to write
Better, faster, stronger
Behat steps
Thank you :)
Rodrigue Villetard
@gorghoa on twitter/github
https://rodrigue.villetard.tech
Développeur freelance
Sept. 2017,
ApéroPHP, Amiens

More Related Content

PPT
Qtp training session V
PDF
Vladyslav Romanchenko "How to keep high code quality without e2e tests"
PDF
Grand Rapids PHP Meetup: Behavioral Driven Development with Behat
PPTX
Zend con 2016 bdd with behat for beginners
PPTX
PHPConf.asia 2016 - BDD with Behat for Beginners
PPTX
Creating better behat features
PDF
[drupalday2017] - Behat per Drupal: test automatici e molto di più
ODP
Behat Workshop at WeLovePHP
Qtp training session V
Vladyslav Romanchenko "How to keep high code quality without e2e tests"
Grand Rapids PHP Meetup: Behavioral Driven Development with Behat
Zend con 2016 bdd with behat for beginners
PHPConf.asia 2016 - BDD with Behat for Beginners
Creating better behat features
[drupalday2017] - Behat per Drupal: test automatici e molto di più
Behat Workshop at WeLovePHP

Similar to Write better, stronger, faster behat steps with ScenarioStateBehatExtension (13)

PDF
BDD API Tests with Gherkin and Behat
PPTX
Automated tests to a REST API
PDF
BDD API Tests with Gherkin and Behat
PDF
Functional testing with behat
PDF
BDD in PHP - Behat
PPTX
BDD in my team: how we do it
KEY
BDD with Behat and Symfony2
PDF
Behat internals for advanced usage. Symfony Camp 2016
PPTX
Php[tek] 2016 - BDD with Behat for Beginners
PDF
Behat dpc12
PPTX
Behat - Drupal South 2018
PDF
Behavioral driven development with Behat
PDF
PHPSpec & Behat: Two Testing Tools That Write Code For You (#phptek edition)
BDD API Tests with Gherkin and Behat
Automated tests to a REST API
BDD API Tests with Gherkin and Behat
Functional testing with behat
BDD in PHP - Behat
BDD in my team: how we do it
BDD with Behat and Symfony2
Behat internals for advanced usage. Symfony Camp 2016
Php[tek] 2016 - BDD with Behat for Beginners
Behat dpc12
Behat - Drupal South 2018
Behavioral driven development with Behat
PHPSpec & Behat: Two Testing Tools That Write Code For You (#phptek edition)
Ad

Recently uploaded (20)

PDF
Soil Improvement Techniques Note - Rabbi
PPTX
Information Storage and Retrieval Techniques Unit III
PPTX
wireless networks, mobile computing.pptx
PPT
Chapter 1 - Introduction to Manufacturing Technology_2.ppt
PDF
Design of Material Handling Equipment Lecture Note
PDF
Cryptography and Network Security-Module-I.pdf
PPTX
Management Information system : MIS-e-Business Systems.pptx
PPTX
Module 8- Technological and Communication Skills.pptx
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PDF
Applications of Equal_Area_Criterion.pdf
PDF
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
PDF
Prof. Dr. KAYIHURA A. SILAS MUNYANEZA, PhD..pdf
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PPTX
Software Engineering and software moduleing
PPTX
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
PPTX
Petroleum Refining & Petrochemicals.pptx
PPTX
Amdahl’s law is explained in the above power point presentations
PDF
Computer organization and architecuture Digital Notes....pdf
PPTX
Chapter 2 -Technology and Enginerring Materials + Composites.pptx
Soil Improvement Techniques Note - Rabbi
Information Storage and Retrieval Techniques Unit III
wireless networks, mobile computing.pptx
Chapter 1 - Introduction to Manufacturing Technology_2.ppt
Design of Material Handling Equipment Lecture Note
Cryptography and Network Security-Module-I.pdf
Management Information system : MIS-e-Business Systems.pptx
Module 8- Technological and Communication Skills.pptx
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
Applications of Equal_Area_Criterion.pdf
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
Prof. Dr. KAYIHURA A. SILAS MUNYANEZA, PhD..pdf
Exploratory_Data_Analysis_Fundamentals.pdf
Software Engineering and software moduleing
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
Petroleum Refining & Petrochemicals.pptx
Amdahl’s law is explained in the above power point presentations
Computer organization and architecuture Digital Notes....pdf
Chapter 2 -Technology and Enginerring Materials + Composites.pptx
Ad

Write better, stronger, faster behat steps with ScenarioStateBehatExtension

  • 1. Better, faster, stronger behat steps with ScenarioStateBehatExtension
  • 2. Behavior Driven Development GOAL: Validate the features/contracts, not the implementation details (a practical definition) Unit tests e2e Acceptance Integration tests Somw’here around there From API level to GUI!
  • 3. Gherkin : A Domain Specific Language to the rescue
  • 4. FEATURE Description of what should beexpected from a specific aspectof the system. GIVEN The system under test is setup to match a specific and benevolent context where the test can be confidently executed. SCENARIO A sequence of    stepsthat verify t  the correct           behavior of the       system. WHEN The system under test ischallenged via its exposedpossible interactions (API,GUI, etc.). THEN The test runner analyze the system under test’s reaction and verify some assertions.
  • 7. State management : Where does it belong?
  • 8. When the system under test is stateful (Like most Single Page Apps or Good’ol server side session_start() websites) State is chiefly handled by the system itself. By running the steps in sequence we can safely assume that the context in which our steps are executed is correct and consistent. This is the kind of environments documentations and blogs usually use to illustrate what they have to say. The easy part :
  • 9. Stateless Systems (API) By definition each interaction with the system is one shot and the system keeps absolutely no track of what is going on. Therefore, state management must be done by the client, ie. our tests implementations. The not so easy:
  • 10. Out of the box solution: CONTEXTS Behat contexts are destroyed and recreated between each scenario. Then, simply use: $this->myStateFragment = $value; may very well be enough in most cases. The not so easy:But… …sharing state fragments amongst multiple contexts quickly becomes painful: Strong coupling (it’s bad) is induced between contexts (this one can be solved by writing a specific context whose responsibility is solely storing state fragments). The dependency to a state fragment by a step is hidden in the step implementation and can not be deduced from step params. The state management logic is dispatched in steps implementation, it adds unnecessary visual debt and dilute the core of the step.
  • 11. OH!
  • 13. Features: ● Share state fragments with all steps for a one (and only one) scenario; ● Inject state fragment directly to steps functions as arguments; ● Fails fast if context is unstable (missing state fragment dependency for a step); ● Reduce the steps code to the bare necessities; ● Leverage PHP type hints; gorghoa/ScenarioStateBehatExtension
  • 14. Set up : Flag a Context as state management aware
  • 15. Share a state fragment
  • 16. Consume a state fragment
  • 17. And yeah, that is all, folks: A simple yet effective way to manage scenario states, letting you to write Better, faster, stronger Behat steps
  • 18. Thank you :) Rodrigue Villetard @gorghoa on twitter/github https://rodrigue.villetard.tech Développeur freelance Sept. 2017, ApéroPHP, Amiens