Learn the basics of Behavioral Driven Development (BDD) with Behat to build quality applications. Behat utilizes natural language syntax to define feature test scenarios. In this tutorial you will learn how to write integration tests for web applications. This will include utilizing Selenium WebDriver for real world multi-browser testing including introductions to Selenium Grid and Sauce Labs. Learn a better way to perform integration testing today!
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Php[tek] 2016 - BDD with Behat for Beginners
1.
2. https://launchkey.com
W elcom e t o
p h p [ t ek ] 2 0 1 6
R a t e Ta lk s:
https://joind.in/event/phptek-2016
Tw it t er :
#phptek
W ifi :
Sheraton Meetings
4. Session Breakdown
Preface (this)
Intro to BDD
Intro to Behat
Build some Tests
Selenium Server/Grid*
Sauce Labs*
Evolution of A Feature*
* Time permitting
https://launchkey.com
5. Who Are You?
What is your role in the development lifecycle?
What is your experience level with PHP, Behat,
and BDD?
https://launchkey.com
6. Pre-Run Checklist
Make sure you have:
PHP 5.5+
Composer
A browser with the Selenium driver
Selenium Server Standalone (Optional)
https://launchkey.com
7. Create a Project
Make a project directory
Run “composer init”
Follow the prompts
For the requirements...
https://launchkey.com
8. Add Your Requirements
1. Add the following
packages to your
project:
behat/behat
behat/mink-extension
behat/mink-goutte-driver
behat/mink-selenium2-
driver
2. Run “composer
install”
https://launchkey.com
9. Verify Your Install
Run “vendor/bin/behat -h”
You should see the help text
https://launchkey.com
12. 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.
https://launchkey.com
13. 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
https://launchkey.com
14. What It 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
https://launchkey.com
15. Failed to Assert False is True
or
How I learned to love BDD
https://launchkey.com
16.
17. 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.
https://launchkey.com
19. Writing Features
Applications are comprised of features
BDD discretely separates each feature
Each feature contains a narrative and a scenario
Feature may also contain a background to set the
stage for the feature scenarios
https://launchkey.com
20. 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?
https://launchkey.com
21. 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.
https://launchkey.com
22. Scenario
A scenarios should contain a 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.
https://launchkey.com
23. 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.
https://launchkey.com
24. 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
https://launchkey.com
25.
26. 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
https://launchkey.com
27. Installation
PHAR installation
Single global install
One version for feature contexts
Project Installation
Composer based install as dependency
Version and dependencies tied to project
https://launchkey.com
29. CLI Executable
Allows for initializing the environment
Running features
Features and scenarios filterable by tags
Chooses environment
Listing step definitions
https://launchkey.com
30. 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
https://launchkey.com
31. 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.
https://launchkey.com
32. 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
https://launchkey.com
33. 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
https://launchkey.com
34. 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
https://launchkey.com
35. 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);
}
https://launchkey.com
36. Let’s Get Coding!
Make sure you have:
PHP 5.5+
Composer
A browser with the Selenium driver
Selenium Server Standalone
https://launchkey.com
37. Create a Project
Make a project directory
Run “composer init”
Follow the prompts
For the requirements...
https://launchkey.com
38. Add Requirements
1. Add the following
packages to your project:
behat/behat
behat/mink-extension
behat/mink-goutte-driver
behat/mink-selenium2-
driver
2. Run “composer install”
https://launchkey.com
39. Verify Your Install
Run “vendor/bin/behat –h”
You should see the help text
https://launchkey.com
43. 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
https://launchkey.com
44. Lets Write A Feature
Requirement:
In order to use the site
As a site visitor
I can access the website
https://launchkey.com
45. Let’s Check Your Work
How did you word your narrative?
How did you word your scenario?
Did you use existing steps or do you need new
steps?
https://launchkey.com
47. Add 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
https://launchkey.com
48. Let’s Check Your Work
How did you word your narrative?
How did you word your scenarios?
Did you use existing steps or do you need new
steps?
Did you clean up after yourself.
https://launchkey.com
49. Finish Up Labs
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
https://launchkey.com
50. Selenium
Industry standard
Server with remote API
Direct integration with Behat/Mink via Selenium
Driver
Can be used headless with PhantomJS via
GhostDriver implementation
https://launchkey.com
51. Using Selenium Server
Add config to bahat.yml and specify browser
BehatMinkExtension:
browser_name: chrome
goutte: ~
selenium2: ~
Apply @javascript tag to scenarios or a feature
Start Selenium Standalone Server
Run “vendor/bin/behat”
https://launchkey.com
52. Selenium Review
Were you able to get your server running?
Did your tests pass?
Any questions regarding Selenium Server?
https://launchkey.com
53. 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
https://launchkey.com
54. Selenium Grid Example
Start hub:
selenium-server-standalone –role hub
Start nodes
selenium-server-standalone –role node –hub URL
Set the wd_host under selenium2 in behat.yml
https://launchkey.com
55. Hosted Selenium
Special Drivers for Sauce Labs and
BrowserStack
Not well documented but work very well
Allow for a “Grid” style environment without
managing the Grid
https://launchkey.com
56. 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
https://launchkey.com
57. Best Practices
Feature files contain one Feature
Features should be more business than technical focused
Use the three A’s
Arrange – Background – Given
Act – When
Assert - Then
https://launchkey.com
58. 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
https://launchkey.com
61. Business Owner
Brings a very general idea as a requirement.
Hopefully they have an idea of business priority
and impact.
This general idea is represented as a product
backlog item
https://launchkey.com
62. From: Big Boss
To: Project Manager
Proj,
We need a TODO list ASAP! This is top priority. Could
mean billions in revenue. Get me an estimate tomorrow!
Big
https://launchkey.com
63. Scrum Master/BSA
Works with the business owner to flesh out
requirements.
Requirements are very general and generic.
They are only used to determine scope and
assist with sizing during sprint planning.
https://launchkey.com
64. TODO list
In order to keep on track with tasks, as a user, I can
manage my tasks in a TODO list. This will be
accomplished by adding tasks to a task list and being
able to update the completion status of those tasks.
https://launchkey.com
65. Developer
Defines the actual user experience
Takes the very generic business requirements
and creates very specific scenarios for each
feature
Scenarios are analogous to use cases
https://launchkey.com
66. Feature: Task List
As an application user, in order to see my tasks, I will be presented a
ask list.
Scenario: No pre-existing tasks
As a user with no existing tasks, I will see an input field with a
placeholder value of “What needs to be done”. The footer will not be
visible.
Scenario: Pre-existing tasks
As a user with pre-existing tasks, I will see the task input fields and
below it a list of tasks in the order in which they were entered.
https://launchkey.com
67. Test Automation
Takes the User Experience requirements and
builds “feature” files utilizing Gherkin, the
Cucumber DSL for writing test scenarios
Scenarios will be standardized to promote
ubiquitous language conformity and step definition
re-use
Missing step definitions identified
https://launchkey.com
68. Feature: Task List
As an application user, in order to see my tasks, I will be presented a task
list.
Background:
Given I am on the homepage
Scenario: Pre-existing tasks shows list
Given the “Do first” task exists
And the “Do next” task exists
Then the “Do first” task is the first item in the task list
And the “Do next” task is the second item in the task list
https://launchkey.com
69. Feature Acceptance
The Feature files with their scenarios act as acceptance
criteria for development
Once the tests pass, the story is considered complete and
ready for demo to the product owner.
Any features/scenarios that cannot be immediately tested in
automation are tagged as such but are added regardless.
https://launchkey.com
70. Please Rate This Talk
https://joind.in/talk/834ba
https://launchkey.com
The direct correlation between the SOAP box and BDD is the reason BDD is becoming more and more popular.
I never meant to learn BDD. I was just looking for a tool to help me stop breaking a critical legacy business application every time we released a new feature. We had built entire integration testing platforms but they would not give me or the business what we needed to identify defects and were constantly giving false positives and negatives to the brittle nature of the tests. The first step was using feature based testing with Cucumber and then Behat when it became available.
Embracing BDD, like everything else in my career, was a process of enlightenment. It took time and required pain to drive growth.