phpconf.asia 2016 - bdd with behat for beginners

51
BDD w/Beh at for Beginners Adam Englander

Upload: adam-englander

Post on 22-Jan-2017

171 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: PHPConf.asia 2016 - BDD with Behat for Beginners

BDD w/Behat for

BeginnersAdam Englander

Page 2: PHPConf.asia 2016 - BDD with Behat for Beginners

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

Page 3: PHPConf.asia 2016 - BDD with Behat for Beginners

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?

Page 4: PHPConf.asia 2016 - BDD with Behat for Beginners

Pre-Run ChecklistMake sure you have:

PHP 5.6+ComposerA browser with the Selenium driverSelenium Server Standalone (Optional for 2.x)

Page 5: PHPConf.asia 2016 - BDD with Behat for Beginners

GotchasMissing one of these extensions:

curlmbstringopenssl

No default timezone in PHP.ini

Page 6: PHPConf.asia 2016 - BDD with Behat for Beginners

Building for BehaviorIntroduction to Behavioral

Driven Development

Page 7: PHPConf.asia 2016 - BDD with Behat for Beginners

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 8: PHPConf.asia 2016 - BDD with Behat for Beginners

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 9: PHPConf.asia 2016 - BDD with Behat for Beginners

What BDD Doesn’t ProvideThe 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 10: PHPConf.asia 2016 - BDD with Behat for Beginners

Failed to Assert False is Trueor

How I learned to love BDD

Page 11: PHPConf.asia 2016 - BDD with Behat for Beginners

How to write effective featuresStory BDD

Page 12: PHPConf.asia 2016 - BDD with Behat for Beginners

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 13: PHPConf.asia 2016 - BDD with Behat for Beginners

Story BDD HierarchyFeature

BackgroundScenario

Step StepScenario

Step Step

Page 14: PHPConf.asia 2016 - BDD with Behat for Beginners

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 15: PHPConf.asia 2016 - BDD with Behat for Beginners

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 16: PHPConf.asia 2016 - BDD with Behat for Beginners

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 17: PHPConf.asia 2016 - BDD with Behat for Beginners

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 18: PHPConf.asia 2016 - BDD with Behat for Beginners

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 19: PHPConf.asia 2016 - BDD with Behat for Beginners

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 20: PHPConf.asia 2016 - BDD with Behat for Beginners

French and FantasticIntroduction to Behat

Page 21: PHPConf.asia 2016 - BDD with Behat for Beginners

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 22: PHPConf.asia 2016 - BDD with Behat for Beginners

InstallationPHAR installation

Single global installOne version for feature contexts

Project InstallationComposer based install as dependencyVersion and dependencies tied to project

Page 23: PHPConf.asia 2016 - BDD with Behat for Beginners

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

Page 24: PHPConf.asia 2016 - BDD with Behat for Beginners

Command Line ExecutableAllows for initializing the environmentRunning features

Features and scenarios filterable by tagsChooses environment

Listing step definitions

Page 25: PHPConf.asia 2016 - BDD with Behat for Beginners

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

Defines output formattersDefines feature suitesConfigures extensions

Page 26: PHPConf.asia 2016 - BDD with Behat for Beginners

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

Page 27: PHPConf.asia 2016 - BDD with Behat for Beginners

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 28: PHPConf.asia 2016 - BDD with Behat for Beginners

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 29: PHPConf.asia 2016 - BDD with Behat for Beginners

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 30: PHPConf.asia 2016 - BDD with Behat for Beginners

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 31: PHPConf.asia 2016 - BDD with Behat for Beginners

Let’s see Behat at workDemo Time

Page 32: PHPConf.asia 2016 - BDD with Behat for Beginners

Create a ProjectComposer “init”Add requirementsBehat “init”Add Contexts to Config

Page 33: PHPConf.asia 2016 - BDD with Behat for Beginners

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

Page 34: PHPConf.asia 2016 - BDD with Behat for Beginners

Initialize BehatInitialize the behat project via the Behat CLI with the --init flagVerify by running “vendor/bin/behat”

Page 35: PHPConf.asia 2016 - BDD with Behat for Beginners

Add Mink to Config• Add the extension to the default profile• Set the base URL• Add the drivers

Page 36: PHPConf.asia 2016 - BDD with Behat for Beginners

Add Contexts to Config• Add Mink context to get access to pre-

built browser steps• Add project context to make custom

steps

Page 37: PHPConf.asia 2016 - BDD with Behat for Beginners

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 38: PHPConf.asia 2016 - BDD with Behat for Beginners

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

Page 39: PHPConf.asia 2016 - BDD with Behat for Beginners

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

Page 40: PHPConf.asia 2016 - BDD with Behat for Beginners

What About Clean UpRequirement: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

Page 41: PHPConf.asia 2016 - BDD with Behat for Beginners

Steps Need to Be IntuitiveChecking for errors on registration submission will require a custom step with some additional logic

Page 42: PHPConf.asia 2016 - BDD with Behat for Beginners

Add Other Requirements

E-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 43: PHPConf.asia 2016 - BDD with Behat for Beginners

Finish Up Labs/Examples

Server 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 44: PHPConf.asia 2016 - BDD with Behat for Beginners

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

Page 45: PHPConf.asia 2016 - BDD with Behat for Beginners

Using Selenium ServerAdd config to bahat.yml and specify browserApply @javascript tag to scenarios or a featureStart Selenium Standalone ServerRun tests

Page 46: PHPConf.asia 2016 - BDD with Behat for Beginners

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 47: PHPConf.asia 2016 - BDD with Behat for Beginners

Sauce Labs/BrowserStackSpecial Drivers for Sauce Labs and BrowserStackNot well documented but work very wellAllow for a “Grid” style environment without managing the Grid

Page 48: PHPConf.asia 2016 - BDD with Behat for Beginners

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 49: PHPConf.asia 2016 - BDD with Behat for Beginners

Best PracticesFeature files contain one FeatureFeatures should be more business than technical focused

Page 50: PHPConf.asia 2016 - BDD with Behat for Beginners

Best Practices (cont.)Use the three A’s

Arrange – Background – GivenAct – WhenAssert - Then

Page 51: PHPConf.asia 2016 - BDD with Behat for Beginners

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