Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Automated Testing for WordPress

Automated Testing for WordPress

Presented @ WordCamp Varna 2017

Boyan Yordanov

September 02, 2017
Tweet

More Decks by Boyan Yordanov

Other Decks in Programming

Transcript

  1. AUTOMATED TESTING
    FOR WORDPRESS
    1

    View Slide

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

    View Slide

  3. CONFESSION TIME
    3

    View Slide

  4. 4

    View Slide

  5. ...
    "Testing is critical for
    quality"
    5

    View Slide

  6. 6

    View Slide

  7. 7

    View Slide

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

    View Slide

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

    View Slide

  10. 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

    View Slide

  11. 11

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  15. WHICH TYPE TO USE?
    ALL OF THEM
    14

    View Slide

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

    View Slide

  17. TESTING AND WORDPRESS
    16 . 1

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  22. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  26. 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

    View Slide

  27. 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

    View Slide

  28. 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

    View Slide

  29. 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

    View Slide

  30. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  34. 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

    View Slide

  35. 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

    View Slide

  36. /**
    * @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

    View Slide

  37. WORDHAT
    BEHAT FOR WORDPRESS
    25 . 1

    View Slide

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

    View Slide

  39. 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

    View Slide

  40. 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

    View Slide

  41. 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

    View Slide

  42. 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

    View Slide

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

    View Slide

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

    View Slide