|

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

  • |

    WordPress Grand Rapids – September 2018: Show And Tell

    Nicole – BoldGrid Post & Page Builder Free & Premium Versions Not Compatible With Editing on Mobile Not Yet Compatible With Gutenberg Premium Designer Blocks & Sliders BoldGrid Central Free cloud-based WordPress instances for testing things out. Crio WordPress Theme Available on ThemeForest Topher – BigCommerce Provides a plugin that pushes your BigCommerce store content…
  • |

    WordCamp US 2019 – Technical SEO Checklist: How to Optimize a WordPress Site for Search Engine Crawlers – Pam Aungst

    Why SEO is Important For search engines Why Technical SEO is Super Important Search Engines: Crawl / Index / Rank If technical SEO isn’t working search engines can’t crawl/index/rank. Visitors wont’ find your site. The Ultimate SEO Checklist Basic/Traditional Items Domain & CMS Run by a well designed platform Meta Robots Search Engine Visibility checkbox…
  • |

    WordCamp GR 2017 – Computers <3 Structured Data – Steve Grunwell

    Steve Grunwell – https://stevegrunwell.com/ https://github.com/stevegrunwell/structured-data/blob/master/README.md What is Structured Data? Standards to help computers to understand our content. Content is for humans. Structured data is for machines. Structured Data Structured data is all about providing context itemtype/itemprop/content HTML/XML HyperText and eXtensible Markup Language Meant to convey information in a meaningful way Generic layout RSS An extension of…
  • |

    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…
  • |

    WordCamp GR 2017 – WP-API: The Good, the Bad, and the Ugly – J Andrew Scott

    J Andrew Scott – http://rubberchickenfarm.com/ History of APIs RSS Advantages Fast & reliable Easy to consume Almost no technical footprint Disadvantages Read-only Static content No authentication No user-driven content (i.e. comments, favorites, etc) DIY API Advantages Dynamic content User-driven content App & user authentication Roles & permissions based Connected applications Disadvantages DIY OAuth Redundant URI scheme…

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