32
Behaviour Testing and Continuous Integration with Drupal Smith Milner October 6th 2013 Pacific Northwest Drupal Summit

Behaviour Testing and Continuous Integration with Drupal

Embed Size (px)

Citation preview

Page 1: Behaviour Testing and Continuous Integration with Drupal

Behaviour Testing and Continuous Integration with Drupal

Smith Milner

October 6th 2013

Pacific Northwest Drupal Summit

Page 2: Behaviour Testing and Continuous Integration with Drupal

https://github.com/affinitybridge/pnwds_behat

Feel free to ask questions during the talk

Working Example

http://affinitybridge.com

Page 3: Behaviour Testing and Continuous Integration with Drupal

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

Page 4: Behaviour Testing and Continuous Integration with Drupal

Enter Behat.From Stage Right

http://affinitybridge.com

Page 5: Behaviour Testing and Continuous Integration with Drupal

Jonathan Hedstrom and Melissa Anderson's

session on

Quality Assurance using Behat and the Drupal Extension?

http://affinitybridge.com

Page 6: Behaviour Testing and Continuous Integration with Drupal

What is Behat?

● Behaviour Driven Development (BDD)● The framework is written in PHP● Write Tests fast● Human Readable

http://affinitybridge.com

Page 7: Behaviour Testing and Continuous Integration with Drupal

The Rest of the Stack

● Mink● Browser Drivers used by Mink (goutte)● Drupal Extension

http://affinitybridge.com

Page 8: Behaviour Testing and Continuous Integration with Drupal

Drupal Extension

● Drupal Specific Step Definitions● 3 Different Drivers

● blackbox● drupal● drush

● Cleans up● In Active Development

http://affinitybridge.com

Page 9: Behaviour Testing and Continuous Integration with Drupal

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"

The Tests

http://affinitybridge.com

Page 10: Behaviour Testing and Continuous Integration with Drupal

Scenario: Short description of the tests goal

Given some known state

When I perform an action

Then I expect a result

Gherkin

http://affinitybridge.com

Page 11: Behaviour Testing and Continuous Integration with Drupal

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

Page 12: Behaviour Testing and Continuous Integration with Drupal

Annotations

Tag feature sets or individual scenarios

● @javascript● @api● @drush● @reinstall (custom)

http://affinitybridge.com

Page 13: Behaviour Testing and Continuous Integration with Drupal

@javascript

Scenario: Short description of the test goal

Given some known state

When I perform an action with javascript

Then I expect a result

Annotations

http://affinitybridge.com

Page 14: Behaviour Testing and Continuous Integration with Drupal

What Powers the Steps?Where the HAL is the code?

http://affinitybridge.com

Page 15: Behaviour Testing and Continuous Integration with Drupal

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

The Context Class: FeatureContext.php

http://affinitybridge.com

Page 16: Behaviour Testing and Continuous Integration with Drupal

class FeatureContext extends DrupalContext {

/**

* @Given /^I run cron$/

*/

public function iRunCron() {

$this->getDriver()->runCron();

}

}

The Context Class: FeatureContext.php

http://affinitybridge.com

Page 17: Behaviour Testing and Continuous Integration with Drupal

YAY We have Tests!

http://affinitybridge.com

Page 18: Behaviour Testing and Continuous Integration with Drupal

Uh OhThey Always Fail

http://affinitybridge.com

Page 19: Behaviour Testing and Continuous Integration with Drupal

● Run them often

● Make sure to capture the fragile functionality

● Take a look at broken tests right away

Stay on Your Tests

http://affinitybridge.com

Page 20: Behaviour Testing and Continuous Integration with Drupal

● The tests break all the time!

● Other developers don’t run my tests!

● It can take a long time!

But It’s So Hard!

http://affinitybridge.com

Page 21: Behaviour Testing and Continuous Integration with Drupal

Enter Travis CI.From Stage Left

http://affinitybridge.com

Page 22: Behaviour Testing and Continuous Integration with Drupal

● Continuous Integration service● Integrates with Github post commit hooks● Runs a build of your project every commit● Reports Results (email, chat client, etc)

What is Travis CI?

http://affinitybridge.com

Page 23: Behaviour Testing and Continuous Integration with Drupal

Where did the name Travis originate?

Bob the Builder

http://affinitybridge.com

Page 24: Behaviour Testing and Continuous Integration with Drupal

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.

How to Travis CI

http://affinitybridge.com

Page 25: Behaviour Testing and Continuous Integration with Drupal

Setup Tasks Include

http://affinitybridge.com

● 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!

Page 26: Behaviour Testing and Continuous Integration with Drupal

Initial Hurdles

http://affinitybridge.com

● Running javascript in a headless environment● Extensive use of 3rd party libraries● Testing configuration on Travis can take a

long time

Page 27: Behaviour Testing and Continuous Integration with Drupal

Travis CI Reports Directly into our Chat Client

Immediate announcement of how good you are at coding

http://affinitybridge.com

Page 28: Behaviour Testing and Continuous Integration with Drupal

Travis CI Build History

Lets you quickly review status of your build.

http://affinitybridge.com

Page 29: Behaviour Testing and Continuous Integration with Drupal

This is me now

http://affinitybridge.com

Page 30: Behaviour Testing and Continuous Integration with Drupal

Example Time!Make it go

http://affinitybridge.com

Page 31: Behaviour Testing and Continuous Integration with Drupal

http://affinitybridge.com

Questions?

Page 32: Behaviour Testing and Continuous Integration with Drupal

http://affinitybridge.com

Thank You!