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. $> WHOAMI Boyan Yordanov Twitter: @specter_bg Github: boyanyordanov Developer @

    Shtrak Organizer of PHP Varna Member of VarnaLab 2
  2. 4

  3. 6

  4. 7

  5. -- some of you will say “But we do test.

    We spend a lot of time testing!” 9
  6. 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
  7. 11

  8. FOR PLUGINS Unit tests as much as possible, End-To- End

    for specific UI functionality. 16 . 3
  9. 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
  10. GHERKIN A Business Readable, Domain Specific Language that lets you

    describe behaviour. behat.org/en/latest/user_guide/gherkin.html 20 . 1
  11. Describes the behavior of the system Acts as documentation Business

    people understand it as well Not concerned with the actual code 20 . 2
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. GHERKIN SPEAKS MANY LANGUAGES #language: bg Функционалност: Информация за групата

    Като потенциален член на групата искам да мога да открия информация за нея Сценарий: показване на информация за групата Когато съм на началната страница То трябва да виждам "PHP Varna" И трябва да виждам "PHP User Group of Varna, Bulgaria." 21 . 6
  18. BEHAT CONTEXTS Match Gherkin with actual test code Just PHP

    classes Can hook into Behat's lifecycle 23
  19. 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
  20. 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
  21. /** * @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
  22. 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
  23. 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
  24. 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
  25. 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