Alternatives to PHPUnit when testing WordPress

Introduction

When talking about automated testing, many think about unit testing with PHPUnit. PHPUnit is great for testing logic in Laravel applications or maybe even WordPress plugins. But when building WordPress sites many alternatives are easier to set up and are better suited for testing content-focused sites.

With these alternatives, you still get the benefit of increased confidence in your code and it’s automated. All of these methods can be run locally or using Branch recipes.

Automated browser tests with Cypress

Cypress.io is an end-to-end testing tool for browsers. It works by creating a new browser window and running through the tasks you’ve specified.

You can test workflows like filling out a form or navigating around a website.

What’s important is that it’s emulating the experience a user would have and make sure everything works (or doesn’t work if that’s what you want to test).

Cypress tests are written in Javascript. For each test you can specify tasks it should do. Examples of this could be visiting a certain page, find a DOM element or interact with the page by typing or clicking.

Cypress has some extra features that make it easy to debug tests that are failing. It can record a video of all tests so you can see what happens if an error occurs. You also have access to the browser environment where the test was run so you can inspect the state of the application at different stages of the test.

When to use Cypress

To test key functionality such as:

  • Forms, including form validation
  • Flows

To test interactive elements such as:

  • Modals and lightboxes
  • Galleries

Test broken Javascript code with ESLint

ESLint is a linting tool for Javascript code. It can check all your .js files for syntax errors, unused variables and code style.

There are many existing configurations for ESLint. A good example is eslint-config-airbnb. Before deciding on a configuration or building your own you should decide what you want it to do.

If you just want to check for syntax errors it might be better to create a small config file from scratch. If you want an opinionated linter that enforces things like semicolons or line breaks then it might be better to find an existing configuration.

If you choose the latter you should be prepared to run ESLint locally as well to automatically fix errors in your code.

When to use ESLint

  • If you use a lot of Javascript in your project
  • If you want to check for syntax errors in Javascript
  • If you want to unify Javascript code style in your team

SEO, speed and accessibility with Lighthouse

Lighthouse is an automated quality assurance tool for measuring things like search engine optimization, performance, and other things.

Lighthouse runs a set of tests in 5 different categories.
Each test receives a score and the category gets an average score from all the tests.

We have a guide that goes more in-depth with Lighthouse.

When to use Lighthouse

  • If you want to make sure your website is optimized for search engines
  • If performance is important
  • If you want to make sure the site is accessible

Test broken PHP code

The PHP command-line tool has a built-in command to lint .php files. This can be utilized to check whether there are any syntax errors in the code.

The following command will lint a PHP file:

php -l index.php

To run this on all files you can create a bash script that will loop over all files in the directory.

Branch has a recipe that automatically checks all your files.

When to use PHP lint

  • If you want to test for broken syntax in PHP files

Testing for outdated PHP code

With PHP_CodeSniffer and PHPCompatibility you can check if you are using deprecated PHP features.

You specify what version of PHP you are running on your server and the code sniffer will warn you about outdated code.

It can also warn you if you are using newer PHP features that have been added in later versions than the one you are using.

To run PHPCompatibility you first need to install PHP_CodeSniffer and then PHPCompatibility as an extension to it.

Then you can run the following command:

phpcs /path/to/code-directory

Branch has a recipe for running PHPCompatibility.

When to use PHPCompatibility

  • If you want to make sure you are not using outdated features
  • If you want to make sure you are not using features added in later PHP versions

Testing coding standards

With PHP_CodeSniffer there are several ways to test for coding standards.

This can be useful if you want to make sure you follow best practices or if you’ve developed your own standards that you want your team to adhere to.

The WordPress organization has developed WordPress Coding Standards which is an opinionated code style for the WordPress core and adjacent projects such as WordPress docs.

Another option is to test for PSR compatibility. PSR1, PSR2, and PSR12 are automatically installed when you install PHP_CodeSniffer.

When to use coding standards

  • If you want to enforce a coding style on your team
  • If you want to follow the official WordPress code style.

Sign up to Branch

Signing up for a Branch account is free and easy. Just head over to branchci.com and sign up with GitHub, Bitbucket or GitLab.

» Sign up to Branch

With a free account, you get 20 builds every month. If you need more than that you can upgrade to a paid account.

Need Help?

We hope this guide has been helpful, but if you still have any questions or feedback, or if you just want to chat about CI/CD, please reach out by clicking the chat icon or by email at hi@branchci.com. Don’t be a stranger!

© 2018 - 2021 Branch Continuous Integration, Inc. All rights reserved.