Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Welcome to Las Vegas!
Session Breakdown
Preface (this)
Intro to BDD
Intro to Behat
Build some Tests
Why Are You Here?
What is your role in the development lifecycle?
What is your reason for attending this session?
What do ...
Pre-Run Checklist
Make sure you have:
PHP 5.6+ w/Composer
https://github.com/aenglander/bdd-with-behat-for-
beginners
Sele...
Gotchas
Missing one of these extensions:
curl
mbstring
openssl
No default timezone in PHP.ini
What is BDD?
BDD is business driven, user focused, and test first.
It focuses on delivering business value as effectively
...
What BDD Provides
Better understanding of the business requirement for
development and QA
Better understanding of existing...
What BDD Doesn’t Provide
The answer to all your problems
A replacement for unit testing
A replacement for manual testing
S...
Failed to Assert False is True
or
How I learned to love BDD
Stay Focused
It is crucial to the success of BDD to focus on what
value a feature provides to the business.
Always underst...
Story BDD Hierarchy
Feature
Background (optional)
Scenario
Step Step
Scenario
Step Step
Writing Features
Applications are comprised of features
BDD discretely separates each feature into its
very own set of tes...
Narrative
Feature narratives are written similarly to Agile/Scrum
stories and must answer these questions:
What is the bus...
Background
A background should contain steps common to all, or
at least most, scenarios to prepare the application
environ...
Scenario
A scenarios should contain a descriptive narrative
Scenarios work in a manner similar to unit tests by arranging
...
Scenario Outline
Scenario outlines use a common set of steps with
placeholders for data provided in a data table.
They are...
Step
Steps do one of the following:
Arrange the environment
Perform an action
Assert a result of the previous actions
Step...
What is Behat?
Open source Behavior Driven Development
framework for PHP
Official PHP implementation of Cucumber
One of th...
Installation
PHAR installation
Single global install
One version for feature contexts
Project Installation
Composer based ...
Components
Command line executable – behat
Configuration file – behat.yml
Feature context(s)
Feature Files
Command Line Executable
Allows for initializing the environment
Running features
Features and scenarios filterable by tags...
Configuration File
Split up into profiles
(inherit from default)
Configures custom
autoloader path
Defines global tag filt...
Feature Context
Defines steps
May extend other contexts
May access other contexts
May add hooks for pre/post tag, step, sc...
Gherkin
Gherkin is a Domain Specific Language (DSL)
utilized to write Features in Behat. It uses key words
to identify the...
Example Gherkin
Feature: Home Page
Scenario: Login Link
Given I am on the homepage
When I click " Login"
Then I will be on...
Step Backing Code
Method on a feature context
Contains doc block annotated (@) matchers
Support defined in context. Defaul...
Example Step
/**
* Opens homepage
* Example: Given I am on "/"
* Example: When I go to "/"
* Example: And I go to "/”
* @G...
Create a Project
Composer “init”
Add requirements
Behat “init”
Add Contexts to Config
Verify Your Install
Run “vendor/bin/behat –h”
You should see the help text
Initialize Behat
Initialize the behat project via the Behat CLI
with the --init flag
Verify by running “vendor/bin/behat”
Add Mink to Config
• Add the extension to the default profile
• Set the base URL
• Add the drivers
Add Contexts to Config
• Add Mink context to get access to pre-built
browser steps
• Add project context to make custom st...
Verify Configuration
Run “vendor/bin/behat –dl”
You should see a LOT of steps
Run “vendor/bin/behat –di”
You should see ad...
Lets Write A Feature Test
User Story:
In order to use the site
As a site visitor
I can access the website
Let’s Make Another
Requirement:
In order to be a user
As a visitor
I can register for a user account
What About Clean Up
Requirement:
E-Mail Address is the user identifier and must be
unique among all users
We’re going to h...
Steps Need to Be Intuitive
Checking for errors on registration submission
will require a custom step with some additional
...
Add Other Requirements
E-Mail Address is the user identifier and must be
unique among all users
Name is required and captu...
Finish Up Labs/Examples
Server and Behat features found in GitHub:
https://github.com/aenglander/bdd-with-behat-for-
begin...
Selenium
Industry standard
Server with remote API
Direct integration with Behat/Mink via Selenium
Driver
Can be used headl...
Using Selenium Server
Add config to bahat.yml and specify browser
Apply @javascript tag to scenarios or a feature
Start Se...
Selenium Grid
Allows for multiple simultaneous test runs, multiple
browser versions, and multiple operating systems
One Hu...
Sauce Labs/BrowserStack
Special Drivers for Sauce Labs and
BrowserStack
Not well documented but work very well
Allow for a...
Configuring Mink Driver
Poorly documented
Easy to figure out the settings by looking at the
driver factories. See:
vendor/...
Best Practices
Feature files contain one Feature
Features should be more business than
technical focused
Best Practices (cont.)
Use the three A’s
Arrange – Background – Given
Act – When
Assert - Then
Best Practices (cont.)
Make steps visually verifiable. Feature files
should be able to serve as documentation
Clean up aft...
Zend con 2016   bdd with behat for beginners
Zend con 2016   bdd with behat for beginners
Zend con 2016   bdd with behat for beginners
Zend con 2016   bdd with behat for beginners
Zend con 2016   bdd with behat for beginners
Upcoming SlideShare
Loading in …5
×

Zend con 2016 bdd with behat for beginners

190 views

Published on

Learn the basics of behavioral driven development (BDD) with Behat to build high quality and well documented applications. You'll learn how BDD can help you deliver greater business value more efficiently while accurately documenting the functionality of your application along the way. You'll learn how to utilize Behat as your BDD tool. With Behat, you'll create tests for the features in your application by utilizing a natural language syntax called Gherkin backed by PHP code to execute the steps executed in the feature's scenarios.

This will be a hands-on tutorial. You'll learn how to implement BDD for a web application. This will include utilizing Selenium WebDriver for real world multi-browser testing including introductions to Selenium Grid and hosted integration services utilizing Selenium.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Zend con 2016 bdd with behat for beginners

  1. 1. Welcome to Las Vegas!
  2. 2. Session Breakdown Preface (this) Intro to BDD Intro to Behat Build some Tests
  3. 3. Why Are You Here? What is your role in the development lifecycle? What is your reason for attending this session? What do you hope to get out of this session? What is your experience level with PHP, Behat, and BDD?
  4. 4. Pre-Run Checklist Make sure you have: PHP 5.6+ w/Composer https://github.com/aenglander/bdd-with-behat-for- beginners Selenium Server and a browser with the Selenium driver (Optional)
  5. 5. Gotchas Missing one of these extensions: curl mbstring openssl No default timezone in PHP.ini
  6. 6. What is BDD? BDD is business driven, user focused, and test first. It focuses on delivering business value as effectively and efficiently as possible while maintaining consistent quality throughout the entire application. BDD is a methodology built around the principles of lean development, extreme programming, test driven development, and domain driven design.
  7. 7. What BDD Provides Better understanding of the business requirement for development and QA Better understanding of existing features for the business Better communication via a ubiquitous language Real insight into the business effect of a defect
  8. 8. What BDD Doesn’t Provide The answer to all your problems A replacement for unit testing A replacement for manual testing Super easy to implement everywhere right this second A measure of code quality
  9. 9. Failed to Assert False is True or How I learned to love BDD
  10. 10. Stay Focused It is crucial to the success of BDD to focus on what value a feature provides to the business. Always understand the the role of the user and their relationship to the feature. Do not get distracted by the technical aspects of the implementation.
  11. 11. Story BDD Hierarchy Feature Background (optional) Scenario Step Step Scenario Step Step
  12. 12. Writing Features Applications are comprised of features BDD discretely separates each feature into its very own set of tests Separated use cases into scenarios
  13. 13. Narrative Feature narratives are written similarly to Agile/Scrum stories and must answer these questions: What is the business benefit? Who is the beneficiary? What is the action performed?
  14. 14. Background A background should contain steps common to all, or at least most, scenarios to prepare the application environment for the scenarios. Background steps should be tested elsewhere as part of another feature. Steps failing in the background steps will identify that the failure is not related to the feature.
  15. 15. Scenario A scenarios should contain a descriptive narrative Scenarios work in a manner similar to unit tests by arranging the environment necessary to begin the test, perform actions, and then assert that the expected results have been attained Scenarios should not test multiple features. Do not perform actions after assertions.
  16. 16. Scenario Outline Scenario outlines use a common set of steps with placeholders for data provided in a data table. They are a replacement for multiple scenarios in which data is the only differentiator and does not define the feature. Adding sales tax would be an example of a feature in which the data in the action and assertion would be different.
  17. 17. Step Steps do one of the following: Arrange the environment Perform an action Assert a result of the previous actions Steps should be reasonably reusable Steps should be aggregated for simplification when arranging the environment
  18. 18. What is Behat? Open source Behavior Driven Development framework for PHP Official PHP implementation of Cucumber One of the easiest Cucumber implementations to get up and running quickly Good documentation: http://docs.behat.org
  19. 19. Installation PHAR installation Single global install One version for feature contexts Project Installation Composer based install as dependency Version and dependencies tied to project
  20. 20. Components Command line executable – behat Configuration file – behat.yml Feature context(s) Feature Files
  21. 21. Command Line Executable Allows for initializing the environment Running features Features and scenarios filterable by tags Chooses environment Listing step definitions
  22. 22. Configuration File Split up into profiles (inherit from default) Configures custom autoloader path Defines global tag filters Defines output formatters Defines feature suites Configures extensions
  23. 23. Feature Context Defines steps May extend other contexts May access other contexts May add hooks for pre/post tag, step, scenario, feature, and suite execution.
  24. 24. Gherkin Gherkin is a Domain Specific Language (DSL) utilized to write Features in Behat. It uses key words to identify the type of step: Given – Arrange When – Act Then - Assert
  25. 25. Example Gherkin Feature: Home Page Scenario: Login Link Given I am on the homepage When I click " Login" Then I will be on the "LaunchKey | Log in" page
  26. 26. Step Backing Code Method on a feature context Contains doc block annotated (@) matchers Support defined in context. Defaults to Turnip Supports Turnip and regular expressions Can contain examples doc block Can contain descript in the doc block
  27. 27. Example Step /** * Opens homepage * Example: Given I am on "/" * Example: When I go to "/" * Example: And I go to "/” * @Given (I )am on :path * @When (I )go to :path */ public function visitPath($path) { $this->browser->open($path); }
  28. 28. Create a Project Composer “init” Add requirements Behat “init” Add Contexts to Config
  29. 29. Verify Your Install Run “vendor/bin/behat –h” You should see the help text
  30. 30. Initialize Behat Initialize the behat project via the Behat CLI with the --init flag Verify by running “vendor/bin/behat”
  31. 31. Add Mink to Config • Add the extension to the default profile • Set the base URL • Add the drivers
  32. 32. Add Contexts to Config • Add Mink context to get access to pre-built browser steps • Add project context to make custom steps
  33. 33. Verify Configuration Run “vendor/bin/behat –dl” You should see a LOT of steps Run “vendor/bin/behat –di” You should see additional information for the steps
  34. 34. Lets Write A Feature Test User Story: In order to use the site As a site visitor I can access the website
  35. 35. Let’s Make Another Requirement: In order to be a user As a visitor I can register for a user account
  36. 36. What About Clean Up Requirement: E-Mail Address is the user identifier and must be unique among all users We’re going to have to clean up users in a hook
  37. 37. Steps Need to Be Intuitive Checking for errors on registration submission will require a custom step with some additional logic
  38. 38. Add Other Requirements E-Mail Address is the user identifier and must be unique among all users Name is required and captures the users name Password is required and must utilize a verification field to ensure correct password entry
  39. 39. Finish Up Labs/Examples Server and Behat features found in GitHub: https://github.com/aenglander/bdd-with-behat-for- beginners Master branch is Behat Master is tagged to go step by step Server branch is server
  40. 40. Selenium Industry standard Server with remote API Direct integration with Behat/Mink via Selenium Driver Can be used headless with PhantomJS via GhostDriver implementation
  41. 41. Using Selenium Server Add config to bahat.yml and specify browser Apply @javascript tag to scenarios or a feature Start Selenium Standalone Server Run tests
  42. 42. Selenium Grid Allows for multiple simultaneous test runs, multiple browser versions, and multiple operating systems One Hub and Multiple Nodes Uses same app as Standalone Server Can be flakey and hard to manage
  43. 43. Sauce Labs/BrowserStack Special Drivers for Sauce Labs and BrowserStack Not well documented but work very well Allow for a “Grid” style environment without managing the Grid
  44. 44. Configuring Mink Driver Poorly documented Easy to figure out the settings by looking at the driver factories. See: vendor/behat/mink- extension/src/Behat/MinkExtension/ServiceContainer /Driver
  45. 45. Best Practices Feature files contain one Feature Features should be more business than technical focused
  46. 46. Best Practices (cont.) Use the three A’s Arrange – Background – Given Act – When Assert - Then
  47. 47. Best Practices (cont.) Make steps visually verifiable. Feature files should be able to serve as documentation Clean up after yourself Scenarios should be idempotent Spend the time to do it the right way

×