Steve Grunwell - Testing WordPress
|

WordCamp GR 2019 – Confidently Testing WordPress – Steve Grunwell

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

  • / / / /

    WordPress Meetup 3/21/19

    Show & Tell WPRig – Morten Rand-Hendrickson (to be next guest speaker) Bold Grid Cloud WordPress Repo Testing – WordPress Theme/Plugin Tester Poopy.Life – Temporary WordPress Installs BrowserStack – Website/Browser Testing Service Grav – Static Site CMS Statamic – Static Site Generator Hugo – Static Site Generator Strattic – Static Site Generator for WordPress Caldera Forms – WordPress Form Generator https://www.simplystatic.co/ https://www.hardypress.com/ WordPress Related Job Market Job Options Non-Technical WordPress Website Work Content – articles, documentation Editing Strategy Auditing SEO – implementing, auditing, specialization Services Marketing Social media management community engagement Help Others Administration Business Management Project Management Support Non-WordPress Businesses Looking to start something new in WordPress Finding A Job Continue Where You Started Examine Your Experiences Get Out There Have Conversations Getting Hired…
  • /

    WordCamp GR 2017 – Development With Material Design – Matt Christensen

    Matt Christensen – https://www.mattchristensen.net/ History Announced 2014 2015 Most Google apps use it Intended for Android Focused on Native Mobile, web was an afterthought What Is It? – https://material.io/ A metaphor – think paper Uniting Style – look the same everywhere Design as a physical medium Paper and Ink Seams & Edges Motion has meaning Web Features – https://getmdl.io/components/ Cards – base component (could be used as a means to layout “table” data) Drawers Snackbar – nice feedback component, can hide automatically Tables Out of the box there is styling but not responsive jQuery Alternative – https://datatables.net/ Frameworks MDL (Material Design) Materialize – http://materializecss.com/ (stay away from Materialize WP, not much development) MDLWP – http://mdlwp.com/ (most features in commercial) Bootstrap Material Design – http://fezvrasta.github.io/bootstrap-material-design/ How Do You Design? What are you…
  • /

    Lucky 7: Don’t Do Anything Until You Hear This – Joe A Simpson Jr

    http://bit.ly/joesimpsonjr-wcgr Hosting I purchase too much hosting… Start with free and level up as you learn WordPress Compare what you need Considerations Site Traffic eCommerce Site Speed Environmentally conscious Staging Uptime Site Design Why doesn’t my site look like the demo? Setup the test data for the demo Make sure there is support I have a premium theme but I can’t move things around. Page Builder or Traditional Theme? Do you need flexibility or are you OK with the theme restrictions? Accessibility Accessibility makes me angry… Color Links without underline can affect people with color blindness Colors shouldn’t be the only way to convey a message Color contrast testing Start with an Accessible Theme Check with the Tab Key – Does it navigate the site…
  • / / / / /

    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 Theme Development Foundation 6 JointsWP FoundationPress WordPress Management WP All Import DVS, Grand Rapids, MI

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