Php[tek] 2016 - BDD with Behat for Beginners

71
BDD w/Beh at for Beginners Adam Englander

Transcript of Php[tek] 2016 - BDD with Behat for Beginners

Page 1: Php[tek] 2016 - BDD with Behat for Beginners

BDD w/Behat for

BeginnersAdam Englander

Page 2: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Page 3: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Page 4: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Session BreakdownPreface (this)Intro to BDDIntro to BehatBuild some Tests

Selenium Server/Grid*Sauce Labs*Evolution of A Feature*

* Time permitting

Page 5: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Who Are You?What is your role in the development lifecycle?What is your experience level with PHP, Behat, and BDD?

Page 6: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Pre-Run ChecklistMake sure you have:

PHP 5.5+ComposerA browser with the Selenium driverSelenium Server Standalone (Optional)

Page 7: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Create a ProjectMake a project directoryRun “composer init”Follow the promptsFor the requirements...

Page 8: Php[tek] 2016 - BDD with Behat for Beginners

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”

Page 9: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Verify Your InstallRun “vendor/bin/behat -h”You should see the help text

Page 10: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Initialize BehatRun “vendor/bin/behat --init”Behat is initializedVerify by running “vendor/bin/behat”

Page 11: Php[tek] 2016 - BDD with Behat for Beginners

Building for BehaviorIntro to Behavioral

Driven Development

Page 12: Php[tek] 2016 - BDD with Behat for Beginners

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.

Page 13: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 14: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 15: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Failed to Assert False is True

orHow I learned to love BDD

Page 16: Php[tek] 2016 - BDD with Behat for Beginners

How to write effective featuresStory BDD

Page 17: Php[tek] 2016 - BDD with Behat for Beginners

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.

Page 18: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Story BDD HierarchyFeature

BackgroundScenario

Step StepScenario

Step Step

Page 19: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 20: Php[tek] 2016 - BDD with Behat for Beginners

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?

Page 21: Php[tek] 2016 - BDD with Behat for Beginners

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.

Page 22: Php[tek] 2016 - BDD with Behat for Beginners

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.

Page 23: Php[tek] 2016 - BDD with Behat for Beginners

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.

Page 24: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 25: Php[tek] 2016 - BDD with Behat for Beginners

French and FantasticIntroduction to Behat

Page 26: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 27: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

InstallationPHAR installation

Single global installOne version for feature contexts

Project InstallationComposer based install as dependencyVersion and dependencies tied to project

Page 28: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

ComponentsCommand line executable – behatConfiguration file – behat.ymlFeature context(s)Feature Files

Page 29: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

CLI ExecutableAllows for initializing the environmentRunning features

Features and scenarios filterable by tagsChooses environment

Listing step definitions

Page 30: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Configuration FileSplit up into profiles (inherit from default)Configures custom autoloader pathDefines global tag filters

Defines output formattersDefines feature suitesConfigures extensions

Page 31: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Feature ContextDefines stepsMay extend other contextsMay access other contextsMay add hooks for pre/post tag, step, scenario, feature, and suite execution.

Page 32: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 33: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 34: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 35: Php[tek] 2016 - BDD with Behat for Beginners

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); }

Page 36: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Let’s Get Coding!Make sure you have:

PHP 5.5+ComposerA browser with the Selenium driverSelenium Server Standalone

Page 37: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Create a ProjectMake a project directoryRun “composer init”Follow the promptsFor the requirements...

Page 38: Php[tek] 2016 - BDD with Behat for Beginners

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”

Page 39: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Verify Your InstallRun “vendor/bin/behat –h”You should see the help text

Page 40: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Initialize BehatRun “vendor/bin/behat --init”Behat is initializedVerify by running “vendor/bin/behat”

Page 41: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Configure Minkdefault: extensions: Behat\MinkExtension: base_url: http://adam-bdd-with-behat.ngrok.io goutte: ~ selenium2: ~

Page 42: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Add Contextsdefault: suites: default: contexts: - Behat\MinkExtension\Context\MinkContext - FeatureContext

Page 43: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 44: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Lets Write A FeatureRequirement:In order to use the siteAs a site visitorI can access the website

Page 45: Php[tek] 2016 - BDD with Behat for Beginners

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?

Page 46: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Let’s Make AnotherRequirement:In order to be a userAs a visitorI can register for a user account

Page 47: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 48: Php[tek] 2016 - BDD with Behat for Beginners

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.

Page 49: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 50: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

SeleniumIndustry standardServer with remote APIDirect integration with Behat/Mink via Selenium DriverCan be used headless with PhantomJS via GhostDriver implementation

Page 51: Php[tek] 2016 - BDD with Behat for Beginners

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”

Page 52: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Selenium ReviewWere you able to get your server running?Did your tests pass?Any questions regarding Selenium Server?

Page 53: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 54: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 55: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 56: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 57: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 58: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 59: Php[tek] 2016 - BDD with Behat for Beginners

BDD in the real worldFeature Example

Page 60: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Evolution of a Feature

Page 61: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 62: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 63: Php[tek] 2016 - BDD with Behat for Beginners

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.

Page 64: Php[tek] 2016 - BDD with Behat for Beginners

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.

Page 65: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 66: Php[tek] 2016 - BDD with Behat for Beginners

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.

Page 67: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 68: Php[tek] 2016 - BDD with Behat for Beginners

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

Page 69: Php[tek] 2016 - BDD with Behat for Beginners

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.

Page 70: Php[tek] 2016 - BDD with Behat for Beginners

https://launchkey.com

Please Rate This Talk

https://joind.in/talk/834ba