Post on 14-Apr-2017
BDD w/Behat for
BeginnersAdam Englander
https://launchkey.com
https://launchkey.com
https://launchkey.com
Session BreakdownPreface (this)Intro to BDDIntro to BehatBuild some Tests
Selenium Server/Grid*Sauce Labs*Evolution of A Feature*
* Time permitting
https://launchkey.com
Who Are You?What is your role in the development lifecycle?What is your experience level with PHP, Behat, and BDD?
https://launchkey.com
Pre-Run ChecklistMake sure you have:
PHP 5.5+ComposerA browser with the Selenium driverSelenium Server Standalone (Optional)
https://launchkey.com
Create a ProjectMake a project directoryRun “composer init”Follow the promptsFor the requirements...
https://launchkey.com
Add Your Requirements1. Add the following
packages to your project:
behat/behatbehat/mink-extension
behat/mink-goutte-driverbehat/mink-selenium2-driver
2. Run “composer install”
https://launchkey.com
Verify Your InstallRun “vendor/bin/behat -h”You should see the help text
https://launchkey.com
Initialize BehatRun “vendor/bin/behat --init”Behat is initializedVerify by running “vendor/bin/behat”
Building for BehaviorIntro to Behavioral
Driven Development
https://launchkey.com
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
What BDD ProvidesBetter understanding of the business requirement for development and QABetter understanding of existing features for the businessBetter communication via a ubiquitous languageReal insight into the business effect of a defect
https://launchkey.com
What It Doesn’t Provide
The answer to all your problemsA replacement for unit testingA replacement for manual testingSuper easy to implement everywhere right this secondA measure of code quality
https://launchkey.com
Failed to Assert False is True
orHow I learned to love BDD
How to write effective featuresStory BDD
https://launchkey.com
Stay FocusedIt 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
Story BDD HierarchyFeature
BackgroundScenario
Step StepScenario
Step Step
https://launchkey.com
Writing FeaturesApplications are comprised of featuresBDD discretely separates each featureEach feature contains a narrative and a scenarioFeature may also contain a background to set the stage for the feature scenarios
https://launchkey.com
NarrativeFeature 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
BackgroundA 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
ScenarioA scenarios should contain a narrativeScenarios 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 attainedScenarios should not test multiple features. Do not perform actions after assertions.
https://launchkey.com
Scenario OutlineScenario 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
StepSteps do one of the following:
Arrange the environmentPerform an actionAssert a result of the previous actions
Steps should be reasonably reusableSteps should be aggregated for simplification when arranging the environment
French and FantasticIntroduction to Behat
https://launchkey.com
What is Behat?Open source Behavior Driven Development framework for PHPOfficial PHP implementation of CucumberOne of the easiest Cucumber implementations to get up and running quicklyGood documentation: http://docs.behat.org
https://launchkey.com
InstallationPHAR installation
Single global installOne version for feature contexts
Project InstallationComposer based install as dependencyVersion and dependencies tied to project
https://launchkey.com
ComponentsCommand line executable – behatConfiguration file – behat.ymlFeature context(s)Feature Files
https://launchkey.com
CLI ExecutableAllows for initializing the environmentRunning features
Features and scenarios filterable by tagsChooses environment
Listing step definitions
https://launchkey.com
Configuration FileSplit up into profiles (inherit from default)Configures custom autoloader pathDefines global tag filters
Defines output formattersDefines feature suitesConfigures extensions
https://launchkey.com
Feature ContextDefines stepsMay extend other contextsMay access other contextsMay add hooks for pre/post tag, step, scenario, feature, and suite execution.
https://launchkey.com
GherkinGherkin is a Domain Specific Language (DSL) utilized to write Features in Behat. It uses key words to identify the type of step:
Given – ArrangeWhen – ActThen - Assert
https://launchkey.com
Example GherkinFeature: 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
Step Backing CodeMethod on a feature contextContains doc block annotated (@) matchers
Support defined in context. Defaults to TurnipSupports Turnip and regular expressions
Can contain examples doc blockCan contain descript in the doc block
https://launchkey.com
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
Let’s Get Coding!Make sure you have:
PHP 5.5+ComposerA browser with the Selenium driverSelenium Server Standalone
https://launchkey.com
Create a ProjectMake a project directoryRun “composer init”Follow the promptsFor the requirements...
https://launchkey.com
Add Requirements1. Add the following
packages to your project:
behat/behatbehat/mink-extensionbehat/mink-goutte-
driverbehat/mink-selenium2-driver
2. Run “composer install”
https://launchkey.com
Verify Your InstallRun “vendor/bin/behat –h”You should see the help text
https://launchkey.com
Initialize BehatRun “vendor/bin/behat --init”Behat is initializedVerify by running “vendor/bin/behat”
https://launchkey.com
Configure Minkdefault: extensions: Behat\MinkExtension: base_url: http://adam-bdd-with-behat.ngrok.io goutte: ~ selenium2: ~
https://launchkey.com
Add Contextsdefault: suites: default: contexts: - Behat\MinkExtension\Context\MinkContext - FeatureContext
https://launchkey.com
Verify ConfigurationRun “vendor/bin/behat –dl”You should see a LOT of stepsRun “vendor/bin/behat –di”You should see additional information for the steps
https://launchkey.com
Lets Write A FeatureRequirement:In order to use the siteAs a site visitorI can access the website
https://launchkey.com
Let’s Check Your WorkHow 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
Let’s Make AnotherRequirement:In order to be a userAs a visitorI can register for a user account
https://launchkey.com
Add RequirementsE-Mail Address is the user identifier and must be unique among all usersName is required and captures the users namePassword is required and must utilize a verification field to ensure correct password entry
https://launchkey.com
Let’s Check Your WorkHow 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
Finish Up LabsServer and Behat features found in GitHub: https://github.com/aenglander/bdd-with-behat-for-beginnersMaster branch is BehatMaster is tagged to go step by stepServer branch is server
https://launchkey.com
SeleniumIndustry standardServer with remote APIDirect integration with Behat/Mink via Selenium DriverCan be used headless with PhantomJS via GhostDriver implementation
https://launchkey.com
Using Selenium ServerAdd config to bahat.yml and specify browserBehat\MinkExtension: browser_name: chrome goutte: ~ selenium2: ~Apply @javascript tag to scenarios or a featureStart Selenium Standalone ServerRun “vendor/bin/behat”
https://launchkey.com
Selenium ReviewWere you able to get your server running?Did your tests pass?Any questions regarding Selenium Server?
https://launchkey.com
Selenium GridAllows for multiple simultaneous test runs, multiple browser versions, and multiple operating systemsOne Hub and Multiple NodesUses same app as Standalone ServerCan be flakey and hard to manage
https://launchkey.com
Selenium Grid Example
Start hub:selenium-server-standalone –role hub
Start nodesselenium-server-standalone –role node –hub URL
Set the wd_host under selenium2 in behat.yml
https://launchkey.com
Hosted SeleniumSpecial Drivers for Sauce Labs and BrowserStackNot well documented but work very wellAllow for a “Grid” style environment without managing the Grid
https://launchkey.com
Configuring Mink Driver
Poorly documentedEasy to figure out the settings by looking at the driver factories. See:
vendor/behat/mink-extension/src/Behat/MinkExtension/ServiceContainer/Driver
https://launchkey.com
Best PracticesFeature files contain one FeatureFeatures should be more business than technical focusedUse the three A’s
Arrange – Background – GivenAct – WhenAssert - Then
https://launchkey.com
Best Practices (cont.)Make steps visually verifiable. Feature files should be able to serve as documentationClean up after yourselfScenarios should be idempotentSpend the time to do it the right way
BDD in the real worldFeature Example
https://launchkey.com
Evolution of a Feature
https://launchkey.com
Business OwnerBrings 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
From: Big BossTo: Project ManagerProj,We need a TODO list ASAP! This is top priority. Could mean billions in revenue. Get me an estimate tomorrow!Big
https://launchkey.com
Scrum Master/BSAWorks 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
TODO listIn 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
DeveloperDefines the actual user experienceTakes the very generic business requirements and creates very specific scenarios for each featureScenarios are analogous to use cases
https://launchkey.com
Feature: Task ListAs an application user, in order to see my tasks, I will be presented a task list.Scenario: No pre-existing tasksAs 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 tasksAs 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
Test AutomationTakes the User Experience requirements and builds “feature” files utilizing Gherkin, the Cucumber DSL for writing test scenariosScenarios will be standardized to promote ubiquitous language conformity and step definition re-useMissing step definitions identified
https://launchkey.com
Feature: Task ListAs 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 listGiven the “Do first” task existsAnd the “Do next” task existsThen the “Do first” task is the first item in the task listAnd the “Do next” task is the second item in the task list
https://launchkey.com
Feature AcceptanceThe Feature files with their scenarios act as acceptance criteria for developmentOnce 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
Please Rate This Talk
https://joind.in/talk/834ba
https://launchkey.com
adam@launchkey.comadam_englander on Twitter#aenglander on Freenodeaenglander on GitHubhttps://github.com/aenglander/bdd-with-behat-for-beginnershttps://joind.in/talk/834ba