Behaviour Testing and Continuous Integration with Drupal Smith Milner October 6th 2013 Pacific Northwest Drupal Summit
Working Example https://github.com/ affinitybridge/pnwds_behat Feel free to ask questions during the talk http://affinitybridge.com
The Problem ● ● ● ● ● Large Complicated Project Multiple Clients using the same codebase Multiple Combinations of features installed Many Deployments Breakage would happen in other areas then the newest code being released http://affinitybridge.com
From Stage Right Enter Behat. http://affinitybridge.com
Jonathan Hedstrom and Melissa Anderson's session on Quality Assurance using Behat and the Drupal Extension? http://affinitybridge.com
What is Behat? ● ● ● ● Behaviour Driven Development (BDD) The framework is written in PHP Write Tests fast Human Readable http://affinitybridge.com
The Rest of the Stack ● ● ● Mink Browser Drivers used by Mink (goutte) Drupal Extension http://affinitybridge.com
Drupal Extension ● ● Drupal Specific Step Definitions 3 Different Drivers ● ● ● ● ● blackbox drupal drush Cleans up In Active Development http://affinitybridge.com
The Tests Scenario: anonymous users can't see user profiles. Given I am an anonymous user When I am viewing a "profile" node Then I should see "Access Denied" http://affinitybridge.com
Gherkin Scenario: Short description of the tests goal Given some known state When I perform an action Then I expect a result http://affinitybridge.com
Where are the tests? ● ● ● The tests are broken up into sets called Features (yup another one). They live in /features at the root of your testing stack. Don’t let your .features get too big. http://affinitybridge.com
Annotations Tag feature sets or individual scenarios ● @javascript ● @api ● @drush ● @reinstall (custom) http://affinitybridge.com
Annotations @javascript Scenario: Short description of the test goal Given some known state When I perform an action with javascript Then I expect a result http://affinitybridge.com
Where the HAL is the code? What Powers the Steps? http://affinitybridge.com
The Context Class: FeatureContext. php Lives in the same directory as in: test_root/features/bootstrap/FeatureContext.php Feature Context is a object created for each scenario. ● Allows sharing of variables between steps ● It extends a Context provided by Drupal Extension ● http://affinitybridge.com
The Context Class: FeatureContext. php class FeatureContext extends DrupalContext { /** * @Given /^I run cron$/ */ public function iRunCron() { $this->getDriver()->runCron(); } } http://affinitybridge.com
YAY We have Tests! http://affinitybridge.com
Uh Oh They Always Fail http://affinitybridge.com
Stay on Your Tests ● Run them often ● Make sure to capture the fragile functionality ● Take a look at broken tests right away http://affinitybridge.com
But It’s So Hard! ● The tests break all the time! ● Other developers don’t run my tests! ● It can take a long time! http://affinitybridge.com
From Stage Left Enter Travis CI. http://affinitybridge.com
What is Travis CI? ● ● ● ● Continuous Integration service Integrates with Github post commit hooks Runs a build of your project every commit Reports Results (email, chat client, etc) http://affinitybridge.com
Bob the Builder Where did the name Travis originate? http://affinitybridge.com
How to Travis CI A configuration file is located in the project root. The .travis.yml file tells Travis CI how to set up an environment and how to run the tests. Every build runs from scratch on a fresh VM. http://affinitybridge.com
Setup Tasks Include ● ● ● ● ● ● ● ● ● Create a MySQL Database Downloading Drush Building test framework with composer Setup Drupal with Drush Make Adding a files directory and settings.php Setup Selenium Standalone Server Install drupal (drush site-install) Run a server (drush runserver) Start your tests! http://affinitybridge.com
Initial Hurdles ● ● ● Running javascript in a headless environment Extensive use of 3rd party libraries Testing configuration on Travis can take a long time http://affinitybridge.com
Travis CI Reports Directly into our Chat Client Immediate announcement of how good you are at coding http://affinitybridge.com
Travis CI Build History Lets you quickly review status of your build. http://affinitybridge.com
This is me now http://affinitybridge.com
Make it go Example Time! http://affinitybridge.com
Questions? http://affinitybridge.com
Thank You! http://affinitybridge.com

Behaviour Testing and Continuous Integration with Drupal

  • 1.
    Behaviour Testing andContinuous Integration with Drupal Smith Milner October 6th 2013 Pacific Northwest Drupal Summit
  • 2.
    Working Example https://github.com/ affinitybridge/pnwds_behat Feel freeto ask questions during the talk http://affinitybridge.com
  • 3.
    The Problem ● ● ● ● ● Large ComplicatedProject Multiple Clients using the same codebase Multiple Combinations of features installed Many Deployments Breakage would happen in other areas then the newest code being released http://affinitybridge.com
  • 4.
    From Stage Right EnterBehat. http://affinitybridge.com
  • 5.
    Jonathan Hedstrom andMelissa Anderson's session on Quality Assurance using Behat and the Drupal Extension? http://affinitybridge.com
  • 6.
    What is Behat? ● ● ● ● BehaviourDriven Development (BDD) The framework is written in PHP Write Tests fast Human Readable http://affinitybridge.com
  • 7.
    The Rest ofthe Stack ● ● ● Mink Browser Drivers used by Mink (goutte) Drupal Extension http://affinitybridge.com
  • 8.
    Drupal Extension ● ● Drupal SpecificStep Definitions 3 Different Drivers ● ● ● ● ● blackbox drupal drush Cleans up In Active Development http://affinitybridge.com
  • 9.
    The Tests Scenario: anonymoususers can't see user profiles. Given I am an anonymous user When I am viewing a "profile" node Then I should see "Access Denied" http://affinitybridge.com
  • 10.
    Gherkin Scenario: Short descriptionof the tests goal Given some known state When I perform an action Then I expect a result http://affinitybridge.com
  • 11.
    Where are thetests? ● ● ● The tests are broken up into sets called Features (yup another one). They live in /features at the root of your testing stack. Don’t let your .features get too big. http://affinitybridge.com
  • 12.
    Annotations Tag feature setsor individual scenarios ● @javascript ● @api ● @drush ● @reinstall (custom) http://affinitybridge.com
  • 13.
    Annotations @javascript Scenario: Short descriptionof the test goal Given some known state When I perform an action with javascript Then I expect a result http://affinitybridge.com
  • 14.
    Where the HALis the code? What Powers the Steps? http://affinitybridge.com
  • 15.
    The Context Class:FeatureContext. php Lives in the same directory as in: test_root/features/bootstrap/FeatureContext.php Feature Context is a object created for each scenario. ● Allows sharing of variables between steps ● It extends a Context provided by Drupal Extension ● http://affinitybridge.com
  • 16.
    The Context Class:FeatureContext. php class FeatureContext extends DrupalContext { /** * @Given /^I run cron$/ */ public function iRunCron() { $this->getDriver()->runCron(); } } http://affinitybridge.com
  • 17.
    YAY We haveTests! http://affinitybridge.com
  • 18.
    Uh Oh They AlwaysFail http://affinitybridge.com
  • 19.
    Stay on YourTests ● Run them often ● Make sure to capture the fragile functionality ● Take a look at broken tests right away http://affinitybridge.com
  • 20.
    But It’s SoHard! ● The tests break all the time! ● Other developers don’t run my tests! ● It can take a long time! http://affinitybridge.com
  • 21.
    From Stage Left EnterTravis CI. http://affinitybridge.com
  • 22.
    What is TravisCI? ● ● ● ● Continuous Integration service Integrates with Github post commit hooks Runs a build of your project every commit Reports Results (email, chat client, etc) http://affinitybridge.com
  • 23.
    Bob the Builder Wheredid the name Travis originate? http://affinitybridge.com
  • 24.
    How to TravisCI A configuration file is located in the project root. The .travis.yml file tells Travis CI how to set up an environment and how to run the tests. Every build runs from scratch on a fresh VM. http://affinitybridge.com
  • 25.
    Setup Tasks Include ● ● ● ● ● ● ● ● ● Createa MySQL Database Downloading Drush Building test framework with composer Setup Drupal with Drush Make Adding a files directory and settings.php Setup Selenium Standalone Server Install drupal (drush site-install) Run a server (drush runserver) Start your tests! http://affinitybridge.com
  • 26.
    Initial Hurdles ● ● ● Running javascriptin a headless environment Extensive use of 3rd party libraries Testing configuration on Travis can take a long time http://affinitybridge.com
  • 27.
    Travis CI ReportsDirectly into our Chat Client Immediate announcement of how good you are at coding http://affinitybridge.com
  • 28.
    Travis CI BuildHistory Lets you quickly review status of your build. http://affinitybridge.com
  • 29.
    This is menow http://affinitybridge.com
  • 30.
    Make it go ExampleTime! http://affinitybridge.com
  • 31.
  • 32.