A Sweet Test Suite DrupalCamp Atlanta | A Sweet Test Suite | Dan - - PowerPoint PPT Presentation

a sweet test suite
SMART_READER_LITE
LIVE PREVIEW

A Sweet Test Suite DrupalCamp Atlanta | A Sweet Test Suite | Dan - - PowerPoint PPT Presentation

A Sweet Test Suite DrupalCamp Atlanta | A Sweet Test Suite | Dan Gurin | @dgurin | @CIVICACTIONS A Sweet Test Sweet Dan Gurin Engineer @CivicActions Drupal Camp Asheville Organizer Twitter @dgurin dangur on github, LinkedIn, www, Slack...


slide-1
SLIDE 1

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

A Sweet Test Suite

slide-2
SLIDE 2

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

Dan Gurin

A Sweet Test Sweet

Engineer @CivicActions Drupal Camp Asheville Organizer Twitter @dgurin dangur on github, LinkedIn, www, Slack...

slide-3
SLIDE 3

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

  • 1. Behavior-Driven Development

(BDD) with Behat

  • 2. Visual Regression Testing
  • 3. Unit testing with PHPUnit

Test Driven Development

slide-4
SLIDE 4

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

Behat Drupal Extension

Behavior Driven Development with Behat

  • Plain language
  • Common scenarios
  • Changes & updates
  • Extensible extension
slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

Tags

Behavior Driven Development with Behat

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

○ @beforeNodeCreate ○ @AfterScenario

slide-7
SLIDE 7

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

Drivers

Behavior Driven Development with Behat

Blackbox No privileges Drush @api Can run on a different server Drupal @api More step definitions

slide-8
SLIDE 8

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.

slide-9
SLIDE 9

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

Comments

Behavior Driven Development with Behat

  • # <- Anything on a line followed

by a hashtag

  • Use PhpStorm?

Control or Command Slash / to comment out line(s)

slide-10
SLIDE 10

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

  • Applies to all

Scenarios

  • Create many

items

Background & Tables

Behavior Driven Development with Behat

slide-11
SLIDE 11

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

Contexts

Behavior 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
slide-12
SLIDE 12

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

Custom Contexts

Behavior Driven Development with Behat

FeatureContext.php file CustomContext class

  • 1. Write the step
  • 2. Run the test (it will fail)
  • 3. Copy the generated snippet
slide-13
SLIDE 13

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

Custom Contexts

Behavior Driven Development with Behat

slide-14
SLIDE 14

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

Custom Contexts

Behavior Driven Development with Behat

slide-15
SLIDE 15

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

Custom Contexts

Behavior Driven Development with Behat

  • 1. Delete exception
  • 2. Add code
  • 3. Test again and see if it worked
slide-16
SLIDE 16

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

  • Steps

○ print current url ○ print last response

  • print_r($variable);

Debugging

Behavior Driven Development with Behat

slide-17
SLIDE 17

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

D8 extension extension

Behavior Driven Development with Behat

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

slide-18
SLIDE 18

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

Sub contexts

Behavior Driven Development with Behat

  • Contrib & Custom projects
  • Modules
  • Themes
slide-19
SLIDE 19

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
  • ntributing.html

Contributing

Behavior Driven Development with Behat

slide-20
SLIDE 20

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

  • 1. Behavior-Driven Development

(BDD) with Behat

  • 2. Visual Regression Testing
  • 3. Unit testing with PHPUnit

Test Driven Development

slide-21
SLIDE 21

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
slide-22
SLIDE 22

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

Visual Regression Testing

Visual Regression Testing with WebdriverCSS

slide-23
SLIDE 23

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

Intersection over Union IoU

Visual Regression Testing with WebdriverCSS

slide-24
SLIDE 24

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

  • 1. Behavior-Driven Development

(BDD) with Behat

  • 2. Visual Regression Testing
  • 3. Unit testing with PHPUnit

Test Driven Development

slide-25
SLIDE 25

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

Drupal 8 PHPUnit Suites

Unit Testing with PHPUnit

Name Extend State Requires

Unit UnitTestCase None Code Kernel KernelTestBase Bootstrapped Database Functional BrowserTestBase Booted Webserver Javascript JavascriptTestBase Booted PhantomJS

slide-26
SLIDE 26

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

Unit

Unit Testing with PHPUnit

slide-27
SLIDE 27

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

Assertions

Unit Testing with PHPUnit

  • Equals
  • Same
  • True / False
  • PHPUnit Documentation Appendix A

https://phpunit.de/manual/current/en/phpunit-book.html#appendixes.assertions

slide-28
SLIDE 28

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

Kernel

Unit Testing with PHPUnit

  • Setup
  • Config
slide-29
SLIDE 29

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

Setup

Unit Testing with PHPUnit

  • Beginning of class
  • Applies to all test functions
  • Set up the parent first
slide-30
SLIDE 30

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

Install

Unit Testing with PHPUnit

  • Schema
  • Entity schema
  • Configuration
slide-31
SLIDE 31

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

setUp

Unit Testing with PHPUnit

slide-32
SLIDE 32

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

Install Fields

Unit Testing with PHPUnit

  • Get from configuration management
  • Create

○ Storage ○ Field

slide-33
SLIDE 33

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

Enable modules

Unit Testing with PHPUnit

slide-34
SLIDE 34

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

Functional

Unit Testing with PHPUnit

  • Enable modules
  • Paths!
  • Web assert
slide-35
SLIDE 35

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

Functional

Unit Testing with PHPUnit

slide-36
SLIDE 36

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

JavaScript

Unit Testing with PHPUnit

  • executeScript / evaluateScript
  • switchToIFrame
  • keyDown
  • resizeWindow
  • wait
slide-37
SLIDE 37

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

JavascriptTestBase

Unit Testing with PHPUnit

  • createScreenshot
  • getDrupalSettings
slide-38
SLIDE 38

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

JavaScript Assertions

Unit Testing with PHPUnit

  • assertElement(Not)Visible
  • assertJsCondition
slide-39
SLIDE 39

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

assertSession

Unit Testing with PHPUnit

  • waitFor

AjaxToFinish, Button, Element(Visible), Field, Link

  • waitOnAutocomplete
  • responseContains
  • isVisible
slide-40
SLIDE 40

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

Test Doubles

Unit Testing with PHPUnit

Return a value without the object

slide-41
SLIDE 41

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

Mock Objects

Unit Testing with PHPUnit

slide-42
SLIDE 42

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

Traits

Unit Testing with PHPUnit

  • Code reuse in many classes
  • Groups sets of methods
  • Can not be instantiated
slide-43
SLIDE 43

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

Reflection Trait

Unit Testing with PHPUnit

Test protected / private methods

slide-44
SLIDE 44

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

Trait in action

Unit Testing with PHPUnit

slide-45
SLIDE 45

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

Data Providers

Unit Testing with PHPUnit

  • Run same test

with different datasets

slide-46
SLIDE 46

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

Data Providers

Unit Testing with PHPUnit

  • Named dataset

via associative array

slide-47
SLIDE 47

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

Debugging

Unit Testing with PHPUnit

  • print_r($whatEver);
  • Screenshots
  • Reports
  • Error/failure messages
slide-48
SLIDE 48

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

Running tests

Unit Testing with PHPUnit

  • phpunit with arguments
  • blt:tests phpunit
  • CI
  • @group
  • . = pass, E = error, F = Failure
slide-49
SLIDE 49

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

  • Test failure as

well as success

  • expectException()

○ Code, Message, MessageRegExp

Happy & Sad Paths

Unit Testing with PHPUnit

slide-50
SLIDE 50

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

  • Common setups
  • Nested levels

Base Classes

Unit Testing with PHPUnit

slide-51
SLIDE 51

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

Open Discussion

slide-52
SLIDE 52

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

Thank you.