Automated Testing for WordPress

Automated Testing for WordPress

Presented @ WordCamp Varna 2017

807dcecdfca0e4078d89127cd440c039?s=128

Boyan Yordanov

September 02, 2017
Tweet

Transcript

  1. AUTOMATED TESTING FOR WORDPRESS 1

  2. $> WHOAMI Boyan Yordanov Twitter: @specter_bg Github: boyanyordanov Developer @

    Shtrak Organizer of PHP Varna Member of VarnaLab 2
  3. CONFESSION TIME 3

  4. 4

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

  6. 6

  7. 7

  8. Figuring out what to test and how to test it

    is even more important 8
  9. -- some of you will say “But we do test.

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

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

    suite 12 . 1
  13. Once we know there is a problem we can fix

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

    Tests 13
  15. WHICH TYPE TO USE? ALL OF THEM 14

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

    Gherkin 15
  17. TESTING AND WORDPRESS 16 . 1

  18. FOR SITES AND THEMES Mostly End-To-End tests and Unit tests

    where possible. 16 . 2
  19. FOR PLUGINS Unit tests as much as possible, End-To- End

    for specific UI functionality. 16 . 3
  20. -- almost everyone But my client/PM/boss/cat thinks that all this

    is a waste of time and money 17
  21. ENTER BDD BEAHVIOUR DRIVEN DEVELOPMENT Bridge the gap between developers

    and the bussiness 18 . 1
  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
  23. "A PHP framework for autotesting your bussiness expectations" behat.org 19

  24. GHERKIN A Business Readable, Domain Specific Language that lets you

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

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

    Като потенциален член на групата искам да мога да открия информация за нея Сценарий: показване на информация за групата Когато съм на началната страница То трябва да виждам "PHP Varna" И трябва да виждам "PHP User Group of Varna, Bulgaria." 21 . 6
  32. HOW ARE WE GOING TO GET FROM PLAIN ENGLISH TO

    ACTUAL TESTS? 22
  33. BEHAT CONTEXTS Match Gherkin with actual test code Just PHP

    classes Can hook into Behat's lifecycle 23
  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
  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
  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
  37. WORDHAT BEHAT FOR WORDPRESS 25 . 1

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

    25 . 2
  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
  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
  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
  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
  43. -- Uncle Bob "The only way to go fast is

    to go well." 27
  44. THANK YOU https://bit.ly/wcvar-2017-testing 28