zend con 2016 bdd with behat for beginners

52
BDD w/Beh at for Beginners Adam Englander – ZendCon 2016

Upload: adam-englander

Post on 22-Jan-2017

115 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Zend con 2016   bdd with behat for beginners

BDD w/Behat for

BeginnersAdam Englander – ZendCon 2016

Page 2: Zend con 2016   bdd with behat for beginners

Welcome to Las Vegas!

Page 3: Zend con 2016   bdd with behat for beginners

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

Page 4: Zend con 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 5: Zend con 2016   bdd with behat for beginners

Pre-Run ChecklistMake sure you have:

PHP 5.6+ w/Composerhttps://github.com/aenglander/bdd-with-behat-for-beginnersSelenium Server and a browser with the Selenium driver (Optional)

Page 6: Zend con 2016   bdd with behat for beginners

GotchasMissing one of these extensions:

curlmbstringopenssl

No default timezone in PHP.ini

Page 7: Zend con 2016   bdd with behat for beginners

Building for BehaviorIntroduction to Behavioral

Driven Development

Page 8: Zend con 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 9: Zend con 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 10: Zend con 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 11: Zend con 2016   bdd with behat for beginners

Failed to Assert False is Trueor

How I learned to love BDD

Page 12: Zend con 2016   bdd with behat for beginners

How to write effective featuresStory BDD

Page 13: Zend con 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 14: Zend con 2016   bdd with behat for beginners

Story BDD HierarchyFeature

Background (optional)Scenario

Step StepScenario

Step Step

Page 15: Zend con 2016   bdd with behat for beginners

Writing FeaturesApplications are comprised of featuresBDD discretely separates each feature into its very own set of testsSeparated use cases into scenarios

Page 16: Zend con 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 17: Zend con 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 18: Zend con 2016   bdd with behat for beginners

ScenarioA scenarios should contain a descriptive 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 19: Zend con 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 20: Zend con 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 21: Zend con 2016   bdd with behat for beginners

French and FantasticIntroduction to Behat

Page 22: Zend con 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 23: Zend con 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 24: Zend con 2016   bdd with behat for beginners

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

Page 25: Zend con 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 26: Zend con 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 27: Zend con 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 28: Zend con 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 29: Zend con 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 30: Zend con 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 31: Zend con 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 32: Zend con 2016   bdd with behat for beginners

Let’s see Behat at workDemo Time

Page 33: Zend con 2016   bdd with behat for beginners

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

Page 34: Zend con 2016   bdd with behat for beginners

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

Page 35: Zend con 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 36: Zend con 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 37: Zend con 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 38: Zend con 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 39: Zend con 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 40: Zend con 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 41: Zend con 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 42: Zend con 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 43: Zend con 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 44: Zend con 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 45: Zend con 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 46: Zend con 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 47: Zend con 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 48: Zend con 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 49: Zend con 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 50: Zend con 2016   bdd with behat for beginners

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

Page 51: Zend con 2016   bdd with behat for beginners

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

Arrange – Background – GivenAct – WhenAssert - Then

Page 52: Zend con 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