A Sweet Test Suite - drupalcampatlanta.com · Behavior Driven Development with BehatContributing....

Preview:

Citation preview

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

A Sweet Test Suite

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Dan GurinA Sweet Test Sweet

Engineer @CivicActionsDrupal Camp Asheville OrganizerTwitter @dgurindangur on github, LinkedIn, www, Slack...

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

1. Behavior-Driven Development (BDD) with Behat

2. Visual Regression Testing3. Unit testing with PHPUnit

Test Driven Development

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Behat Drupal ExtensionBehavior Driven Development with Behat

● Plain language● Common scenarios● Changes & updates● Extensible extension

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

TagsBehavior Driven Development with Behat

● Driver @api @javascript● Groups @database @form● Hooks

○ @beforeNodeCreate○ @AfterScenario

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

DriversBehavior Driven Development with Behat

Blackbox No privileges

Drush @api Can run on a different server

Drupal @api More step definitions

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

machine_name * Machine Name

Behavior Driven Development with Behat

Generated by JavaScript, so without PhantonJS & @javascript, becomes an additional required field.

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

CommentsBehavior Driven Development with Behat

● # <- Anything on a line followed by a hashtag

● Use PhpStorm? Control or Command Slash /to comment out line(s)

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

● Applies to allScenarios● Create manyitems

Background& Tables

Behavior Driven Development with Behat

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

ContextsBehavior Driven Development with Behat

● Drupal - users, terms, and nodes○ DrupalRaw - functionality

● Mink - regions and forms● Markup - tags, classes, & attributes● Message - notice, warning, & error● Drush - call commands directly

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Custom ContextsBehavior Driven Development with Behat

FeatureContext.php file CustomContext class

1. Write the step2. Run the test (it will fail)3. Copy the generated snippet

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Custom ContextsBehavior Driven Development with Behat

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Custom ContextsBehavior Driven Development with Behat

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Custom ContextsBehavior Driven Development with Behat

1. Delete exception2. Add code3. Test again and see if it worked

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

● Steps○ print current url○ print last response

● print_r($variable);

DebuggingBehavior Driven Development with Behat

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

D8 extension extensionBehavior Driven Development with Behat

github.com/nuvoleweb/drupal-behatAutocomplete, CKEditor, Chosen, Content, Drupal, Menu, Position, Raw, Responsive, Screenshot, Select, ServiceContainer, Taxonomy, Visibility

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Sub contextsBehavior Driven Development with Behat

● Contrib & Custom projects● Modules● Themes

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

● Behat Drupal Extension● https://github.com/Behat/Behat/

blob/master/CONTRIBUTING.md● http://mink.behat.org/en/latest/c

ontributing.html

ContributingBehavior Driven Development with Behat

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

1. Behavior-Driven Development (BDD) with Behat

2. Visual Regression Testing3. Unit testing with PHPUnit

Test Driven Development

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Visual Regression Testing

Visual Regression Testing with WebdriverCSS

● Good coverage● Display changes● Great for updates

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Visual Regression Testing

Visual Regression Testing with WebdriverCSS

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Intersection over Union IoUVisual Regression Testing with WebdriverCSS

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

1. Behavior-Driven Development (BDD) with Behat

2. Visual Regression Testing3. Unit testing with PHPUnit

Test Driven Development

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Drupal 8 PHPUnit SuitesUnit Testing with PHPUnit

Name Extend State RequiresUnit UnitTestCase None Code

Kernel KernelTestBase Bootstrapped Database

Functional BrowserTestBase Booted Webserver

Javascript JavascriptTestBase Booted PhantomJS

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

UnitUnit Testing with PHPUnit

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

AssertionsUnit Testing with PHPUnit

● Equals● Same● True / False● PHPUnit Documentation Appendix Ahttps://phpunit.de/manual/current/en/phpunit-book.html#appendixes.assertions

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

KernelUnit Testing with PHPUnit

● Setup● Config

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

SetupUnit Testing with PHPUnit

● Beginning of class● Applies to all test functions● Set up the parent first

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Install Unit Testing with PHPUnit

● Schema● Entity schema● Configuration

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

setUp Unit Testing with PHPUnit

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Install Fields Unit Testing with PHPUnit

● Get from configuration management● Create

○ Storage○ Field

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Enable modulesUnit Testing with PHPUnit

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

FunctionalUnit Testing with PHPUnit

● Enable modules● Paths!● Web assert

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

FunctionalUnit Testing with PHPUnit

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

JavaScriptUnit Testing with PHPUnit

● executeScript / evaluateScript● switchToIFrame● keyDown● resizeWindow● wait

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

JavascriptTestBaseUnit Testing with PHPUnit

● createScreenshot● getDrupalSettings

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

JavaScript AssertionsUnit Testing with PHPUnit

● assertElement(Not)Visible● assertJsCondition

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

assertSessionUnit Testing with PHPUnit

● waitForAjaxToFinish, Button, Element(Visible), Field, Link● waitOnAutocomplete● responseContains● isVisible

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Test DoublesUnit Testing with PHPUnit

Return a value without the object

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Mock ObjectsUnit Testing with PHPUnit

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

TraitsUnit Testing with PHPUnit

● Code reuse in many classes● Groups sets of methods● Can not be instantiated

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Reflection TraitUnit Testing with PHPUnit

Test protected / private methods

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Trait in actionUnit Testing with PHPUnit

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Data ProvidersUnit Testing with PHPUnit

● Run same test with different datasets

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Data ProvidersUnit Testing with PHPUnit

● Named dataset via associative array

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

DebuggingUnit Testing with PHPUnit

● print_r($whatEver);● Screenshots● Reports● Error/failure messages

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Running testsUnit Testing with PHPUnit

● phpunit with arguments● blt:tests phpunit● CI● @group● . = pass, E = error, F = Failure

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

● Test failure as well as success

● expectException()○ Code, Message,

MessageRegExp

Happy & Sad PathsUnit Testing with PHPUnit

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

● Common setups● Nested levels

Base ClassesUnit Testing with PHPUnit

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Open Discussion

DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS

Thank you.

Recommended