Launch with Confidence! Behavior Driven Development (BDD) 101 Presented by: Shawn Smiley [Director of Engineering Operations]
About Me LinkedIn: www.linkedin.com/in/shawnsmiley/" Twitter: @shawnsmiley" 3 Shawn S. Smiley Director of Engineering Operations at Achieve Internet 15+ years experience in Internet software and hosting 5+ years with Drupal Recently married
Agenda What is BDD? Why should I use it? How do I use it? Basic Syntax Getting Setup Writing Your First Test Writing custom steps 4
Achieve at a Glance legal name founded headquarters leadership mission primary contact info Achieve Internet, Inc." 1998" 1767 Grand Avenue, Suite #2 San Diego, CA 92109 U.S.A." Ron Huber, Chief Executive Officer" Achieve leverages best-in-class Open Source web application development experience, and tools to create optimized platforms for enterprise clients. " 800.618.8777 info@achieveinternet.com" Achieve is a leading software development company that leverages Drupal to develop advanced enterprise web and mobile solutions for an array of clients in varying verticals. Achieve has a singular goal: To help you launch your advanced digital platforms with confidence. Our ethos is it s not magic, it s excellence, and our all-star developers deliver on that promise with every new project we take on by consistently exceeding client expectations. 5
Key Clients: Media & Entertainment clients! 6
Additional Key Clients clients! 7
Achieve Services Application Development! Achieve builds custom enterprise IT management systems to solve the most complex business challenges. Platform! Integration! Achieve makes integration easy so you can focus on what is most important to your business your customers. Infrastructure Services! Optimize the performance of your web site or application to increase productivity and streamline operations. Achieve has vast experience in creating global web solutions that far out perform the competition. Globalization Services! Mobility & End User Services! Going Mobile is no longer an option. With Achieve you won t have to compromise design for utility. 8
What is BDD? 9
Where does BDD Fit? Type of Tes)ng Examples of Automated Tools When used? Unit Tes0ng phpunit, SimpleTest Tes0ng low level, single func0ons. Func0onal Tes0ng Behavioral Tes0ng Acceptance Tes0ng phpunit, SimpleTest Behat, Cucumber, Fitnesse Behat, Cucumber, Fitnesse Tes0ng larger structures such as classes, services or modules. Tes0ng business requirements. Tes0ng complex/composite func0onality. * All of these types of tes0ng can be done manually Verifying that the final product looks and behaves as designed. 10
What does it look like? 11
Why do BDD? 12
Benefits and Limitations Benefits Allows testing the sum rather than the parts Tests can be written in a human readable format Can test end-user interactions with a site Can test AJAX and other client side scripts (leveraging Selenium) Limitations Not for testing theming or presentation. Setup of the test framework can be a bit complicated to get working. Syntax, while human readable, is still very rigid/strict. 13
How to do BDD? 14
Basic syntax and usage File: features/<feature name>.feature Feature: <feature name> <feature descrip0on> Scenario: <scenario name> Given <there is some condi0on> When <I do something> Then <I should see something> Scenario: <scenario name> Given <there is some condi0on> When <I do something> Then <I should see something> 15
Basic syntax and usage: Example File: features/login.feature 16
Getting Set Up: Components Components: PHP 5.3 Composer (http://getcomposer.org) Behat (http://behat.org) Mink (https://github.com/behat/mink) Java Selenium Server (http://docs.seleniumhq.org) Drupal Library Behat Drupal Extension (https://drupal.org/project/drupalextension) This is the extension Drupal.org uses for their BDD tests and the one this presentation uses. Behat integration with Drupal (https://drupal.org/project/behat) Drupal Behat Testing (https://drupal.org/project/behat_testing) 17
Getting Setup Up: Install Prerequisites PHP 5.3 (I m assuming you already have this) Composer curl -s https://getcomposer.org/installer php Selenium Server Download & install Java for your platform Download Selenium from http://selenium.googlecode.com/files/ selenium-server-standalone-2.39.0.jar 18
Getting Setup Up: Create your project Create a folder for your project (e.g. mybddtests ) WARNING: Do not place this inside your docroot Change into your project folder (cd mybddtests) Create the file composer.json with the following content: { "require": { "drupal/drupal-extension": "*" }, "config": { "bin-dir": "bin/" } } run: php composer.phar install 19
Getting Setup Up: Create your project Create the file behat.yml with the following content: run: bin/behat --init 20
Basic Folder Structure <BDD Project Root> bin Contains the behat and webunit executable files. features Contains your actual test code in *.feature files. bootstrap Contains the site specific PHP scripts for your features. vendor Contains various libraries used by the test framework. 21
Getting Setup Up: Create your project Edit features/bootstrap/featurecontext.php Change the extends statement on the FeatureContext class to be Drupal\DrupalExtension\Context\DrupalContext Verify that everything is setup correctly by running bin/ behat dl You should see a long list of supported test patterns. 22
Getting Setup Up: Create your tests cd features Create a file named login.feature and open it in an editor Add the following content to the login.feature file: 23
Getting Setup Up: Run your test Go to the root of your project folder. Run: bin/behat You should see an output similar to the following: 24
Creating custom steps 25
Creating custom steps Create a new file features/nav_block_visible.feature with the following content: The step Then I should see a block titled Navigation is our new custom step. 26
Creating custom steps Run: bin/behat, you ll see an output similar to the following: Copy the output text and add it to features/bootstrap/ FeatureContext.php and rerun bin/behat. 27
Creating custom steps Set the content of the ishouldseeablocktitled() method: 28
Creating custom steps Rerun behat: bin/behat You should see the following: 29
Success! 30
Behavior Driven Development 31
References Behat - hyp://behat.org Gherkin Language - hyp://docs.behat.org/guides/1.gherkin.html Drupal.org BDD Tests - hyps://drupal.org/project/doobie Drupal.org BDD Tests Documenta0on Page - hyps://drupal.org/node/1551234 Behat Drupal Extension - hyps://drupal.org/project/drupalextension Dan North (Creator of BDD) - hyp://dannorth.net/category/bdd/ Behat Cheatsheet: hyp://blog.lepine.pro/wp- content/uploads/2012/03/behat- cheat- sheet- en.pdf 32
Interested in this topic? Want to know more? Want to be part of the team? Stop by the booth and let s chat! 33
Thank You Shawn Smiley! Director of Engineering Operations" shawn.smiley@achieveinternet.com" Drupal: shawn_smiley" 800.618.8777"
Connect with Achieve If you have any further questions or would like to get in touch with Achieve you can do so on these various channels: info@achieveinternet.com linkedin.com/company/achieve-internet plus.google.com/101805819007866218919 @AchieveInternet facebook.com/pages/achieve-internet 35
Q & A 36