Slide 1

Slide 1 text

AUTOMATED TESTING FOR WORDPRESS 1

Slide 2

Slide 2 text

$> WHOAMI Boyan Yordanov Twitter: @specter_bg Github: boyanyordanov Developer @ Shtrak Organizer of PHP Varna Member of VarnaLab 2

Slide 3

Slide 3 text

CONFESSION TIME 3

Slide 4

Slide 4 text

4

Slide 5

Slide 5 text

... "Testing is critical for quality" 5

Slide 6

Slide 6 text

6

Slide 7

Slide 7 text

7

Slide 8

Slide 8 text

Figuring out what to test and how to test it is even more important 8

Slide 9

Slide 9 text

-- some of you will say “But we do test. We spend a lot of time testing!” 9

Slide 10

Slide 10 text

HOW ARE YOU TESTING? 1. Make a change 2. Switch to a browser 3. Click around to see the new thing 4. Check that nothing is broken? 10

Slide 11

Slide 11 text

11

Slide 12

Slide 12 text

WHAT CAN WE DO ABOUT IT summary of a test suite 12 . 1

Slide 13

Slide 13 text

Once we know there is a problem we can fix it. 12 . 2

Slide 14

Slide 14 text

TYPES OF AUTOMATED TESTING Unit Test Integration Tests Acceptance (End-To-End) Tests 13

Slide 15

Slide 15 text

WHICH TYPE TO USE? ALL OF THEM 14

Slide 16

Slide 16 text

TESTING TOOLS PHP Unit Behat Codeception PHP Spec Selenium Mockery Gherkin 15

Slide 17

Slide 17 text

TESTING AND WORDPRESS 16 . 1

Slide 18

Slide 18 text

FOR SITES AND THEMES Mostly End-To-End tests and Unit tests where possible. 16 . 2

Slide 19

Slide 19 text

FOR PLUGINS Unit tests as much as possible, End-To- End for specific UI functionality. 16 . 3

Slide 20

Slide 20 text

-- almost everyone But my client/PM/boss/cat thinks that all this is a waste of time and money 17

Slide 21

Slide 21 text

ENTER BDD BEAHVIOUR DRIVEN DEVELOPMENT Bridge the gap between developers and the bussiness 18 . 1

Slide 22

Slide 22 text

Think about the behaviour Have conversations with the client/PM Together come up with examples of the correct behaviour Write the test code 18 . 2

Slide 23

Slide 23 text

"A PHP framework for autotesting your bussiness expectations" behat.org 19

Slide 24

Slide 24 text

GHERKIN A Business Readable, Domain Specific Language that lets you describe behaviour. behat.org/en/latest/user_guide/gherkin.html 20 . 1

Slide 25

Slide 25 text

Describes the behavior of the system Acts as documentation Business people understand it as well Not concerned with the actual code 20 . 2

Slide 26

Slide 26 text

Feature: The site should display information about the user group Scenario: See next event on the home page Given there is a scheduled meetup "September Gathering" When I go to the homepage Then I should see "Next Event" And I should see "September Gathering" And I should see "See details" 21 . 1

Slide 27

Slide 27 text

Feature: The site should display information about the user group Scenario: See next event on the home page Given there is a scheduled meetup "September Gathering" When I go to the homepage Then I should see "Next Event" And I should see "September Gathering" And I should see "See details" 21 . 2

Slide 28

Slide 28 text

Feature: The site should display information about the user group Scenario: See next event on the home page Given there is a scheduled meetup "September Gathering" When I go to the homepage Then I should see "Next Event" And I should see "September Gathering" And I should see "See details" 21 . 3

Slide 29

Slide 29 text

Feature: The site should display information about the user group Scenario: See next event on the home page Given there is a scheduled meetup "September Gathering" When I go to the homepage Then I should see "Next Event" And I should see "September Gathering" And I should see "See details" 21 . 4

Slide 30

Slide 30 text

Feature: The site should display information about the user group Scenario: See next event on the home page Given there is a scheduled meetup "September Gathering" When I go to the homepage Then I should see "Next Event" And I should see "September Gathering" And I should see "See details" 21 . 5

Slide 31

Slide 31 text

GHERKIN SPEAKS MANY LANGUAGES #language: bg Функционалност: Информация за групата Като потенциален член на групата искам да мога да открия информация за нея Сценарий: показване на информация за групата Когато съм на началната страница То трябва да виждам "PHP Varna" И трябва да виждам "PHP User Group of Varna, Bulgaria." 21 . 6

Slide 32

Slide 32 text

HOW ARE WE GOING TO GET FROM PLAIN ENGLISH TO ACTUAL TESTS? 22

Slide 33

Slide 33 text

BEHAT CONTEXTS Match Gherkin with actual test code Just PHP classes Can hook into Behat's lifecycle 23

Slide 34

Slide 34 text

CREATE A CONTEXT AND USE HOOKS use \Behat\MinkExtension\Context\RawMinkContext; class FeatureContext extends RawMinkContext { /** @beforeStep */ public function setBrowserSize() { $this->getSession()->resizeWindow(1200, 800); } } 24 . 1

Slide 35

Slide 35 text

DEFINING STEPS /** * @When I click on the ":menuItem" menu item * @When I go to about page */ public function iClickOnTheMenuItem( $menuItem = 'About' ) { $this->getSession()->getPage() ->find('css', "a:contains($menuItem)") ->click(); } 24 . 2

Slide 36

Slide 36 text

/** * @Given /^there is a scheduled meetup "([^"]*)"$/ */ public function thereIsAScheduledMeetup( $name ) { $date = new DateTime(); // P1D means a period of 1 day $date->add( new DateInterval( 'P1D' ) ); $this->scheduleMeetup(compact('name', 'date')); } 24 . 3

Slide 37

Slide 37 text

WORDHAT BEHAT FOR WORDPRESS 25 . 1

Slide 38

Slide 38 text

WordHat is an integration layer between Behat and WordPress. wordhat.info 25 . 2

Slide 39

Slide 39 text

WHAT DO WE GET? Integration with WordPress trough a suite of drivers including wp-cli and the REST api Fully integrated with Behat Behat contexts covering WordPress functionality 25 . 3

Slide 40

Slide 40 text

PRE-DEFINED STEPS Scenario: Viewing a single page Given I am viewing a post: | post_type | post_title | post_content | post_status | | page | My about page | About this site | publish | Then I should see "My about page" And I should see "About this site" 25 . 4

Slide 41

Slide 41 text

ACCESS THE DRIVERS protected function scheduleMeetup( $data ) { $userId = $this->getDriver()->getUserIdFromLogin("boyan"); $result = $this->createContent([ "post_type" => "phpvarna_event", "post_title" => $data['name'], "post_content" => $data['description'], "post_status" => "publish", "post_author" => $userId ]); /** ... */ $this->getDriver()->wpcli('post', 'meta update', [ $result['id'], 'location', $data['location'] ]); } 25 . 5

Slide 42

Slide 42 text

WHAT TO TAKE HOME Proper testing of your work is crucial Talking to the client helps a lot Experiment and find what works for you Tests are code 26

Slide 43

Slide 43 text

-- Uncle Bob "The only way to go fast is to go well." 27

Slide 44

Slide 44 text

THANK YOU https://bit.ly/wcvar-2017-testing 28