|

WordCamp GR 2019 – Confidently Testing WordPress – Steve Grunwell

Steve Grunwell - Testing WordPress
Steve Grunwell - Testing WordPress

https://stevegrunwell.github.io/confidently-testing-wordpress/#/

Testing Fundamentals

Automated Testing

  • Reduces time + chance of human error
  • Easily reproducible
  • Gateway to CI/CD

Test Types

  • Unit – Test the smallest possible unit of an app. Often a single function
  • Integration – How individual components interact
  • End-to-End(E2E) – An entire path through an application

Automated Testing Pyramid

ROI for Testing

System Under Test (SUT)

  • The system we’re currently testing:
    • A single method
    • A class
    • A whole feature

WordPress Testing is Complicated

  • Tightly-coupled system
  • Difficult to test anything in true isolation

Testing Toolbox

PhpUnit – https://phpunit.de/

Structure

  • Test Suite – Collection of test classes
  • Test Class (class) – Collection of test methods
  • Test Case (method) – A single scenario to be tested

Assertions

  • True or False
  • Equality
  • Verify Contents – contains, regular expression matching
  • Negative Assertions

Test Doubles

Replacing actual systems with test versions.

  • Stub out/mock an API
  • Return known values

Mockery – popular library for creating test doubles

PhpUnit Markup Assertions – Assertions powered by DOMDocument

WP Core Test Suite

Scaffolding Out Test Suite

Generate test scaffolding via WP-CLI

What Do We Get?

  • phpunit.xml.dist
  • .phpcs.xml.dist
  • .travis.yml
  • bin/install-wp-tests.sh
  • tests/bootstrap.php
  • tests/test-sample.php

What We Don’t Get

  • Composer

Bootstrap File

  1. Locate the WordPress installation
  2. Gain access to the tests_add_filter() function.
  3. Load the main plugin file.
  4. Bootstrap WordPress core.

Base Test Class

  • HPUnit\Framework\TestCase
  • WP_UnitTestCase

Fixtures

@Group

Data Providers

  • Methods
  • @testWith

Factories (WordPress)

Generate users, posts, and more for testing.

  • factory->post->create()
  • factory->post->create_and_get()
  • factory->post->create_many()

Impersonating Users (WordPress)

Act as users with different roles & capabilities

go_to() (WordPress)

Change the current page context.

Checking for WP_ERRORs

Was the response an instance of WP_ERROR?

Writing Tests

Arrange – Act – Assert

  • Arrange – Set up the scenario
  • Act – Execute the code
  • Assert – Verify things happened as you expected

Testing Permissions

Registering a Custom Post Type

Testing Hooks

Testing Output

Stubbing HTTP Requests

Basic Test-Driven Development (TDD)

Basic Workflow

  1. Write a failing test to describe the functionality or behavior (Red)
  2. Write the code to make the test pass (Green)
  3. Refactor, rinse, repeat (Refactor)

Regression Tests

Similar Posts

  • | |

    Tuesday Interval Training Run And Virtual 5K

    A big shout out to Automattic for promoting the WWWP5K virtual run and matching donations to your favorite charity. #wwwp5k #HWI #Run4Water #WhyIRun #RiverBankRun Automattic matching donations: http://timn.me/wwwp5k2020 Give the gift of clean water: https://timn.me/2020HWI Also on: MastodonMastodon XX InstagramInstagram
  • |

    WordCamp US 2019 – Securing WordPress in the age of 0-Day Vulnerabilities – Rahul Nagare

    scaledynamix.com/WCUS 0-Day Vulnerabilities Recently discovered No current fix Already being attacked Reference – wpvulndb.com Why My Site? They want to send your traffic somewhere else to boost SEO rank They want to use your site as a “bot” to attack a targeted site. How Do You Protect Your Site? Protection Against Redirects Hardcode your site/home…
  • |

    WordCamp GR 2017 – Underscores & Me – Frederick Polk

    Frederick Polk – http://oneblackcrayon.com/ Generate your Underscores Theme: Use a custom build, with the Advanced Options, in order to tailor to your project needs. – https://underscores.me/ Underscores is Pre-built With: Sidebars Custom Logo Comments Featured Images Custom Headers Standard Core Theme Templates (404, comments, archives, header, footer) phpcs Sample Sites: amplifygr.com – Underscores, Visual Composer…
  • | | | | |

    WordPress Meetup – February 2019

    Show & Tell Grammarly – Proofreading Google Docs – for writing Zoho – online productivity suite Invoicely – Small business client management & automation Plutio – Project management WordPress.org Favorites – When installing new plugins you can use favorites and use any WordPress.org username. WordPress Plugins TablePress Advanced Custom Fields Block Lab WordPress Themes Hestia…
  • | |

    WordPress DevOps – WordCamp Grand Rapids 2018

    1. Introductions Development History High School 1996 – Personal School Website – image maps with cgi-bin processing written in C 1996 – Davisco Foods International (First Freelance Job) – static HTML site College 2000 – Inspiration Point Christian Camp & Retreat Center – perl for form processing, PHP template-based site Post-College 2002 – Cross Roads…
  • WordCamp GR 2017 – Plugins: The WP Box of Chocolates – Stacy Vanden Heuvel

    Stacy Vanden Heuvel – http://www.mtnwebs.net/ (blog) http://mtnwebs.com/ What are they? Premanufactured code that adds function and features. 51,000+ plugins available today. Dropped into a site. There is a plugin or everything. Why does anyone care? Makes website building easier. How do you choose? How many active installs? Does it have reputable developers? When was it last…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)