Proper unit testing in WordPress

I think that what you are looking for would actually technically be called functional testing, integration testing, or acceptance testing. It sounds like you want to test the behavior of the front-end of your website (or the front-end behavior of a plugin or theme), not each unit of the code itself. You might use a tool like PHPUnit to test your PHP code, and a tool like QUnit to test your JS code, but you will need a different tool to test the actual behavior of the website (or plugin/theme feature) as an integrated whole.

Unfortunately, acceptance testing is still rather young in the WordPress space. There are a few plugins or themes that do it, but there is no “core” method (but see trac ticket #34693). There isn’t necessarily a consensus “best practice” tool that has emerged yet, either. So, as far as WordPress-specific tools, this is an area that is still being explored. (I myself am still just beginning to explore acceptance testing a WordPress plugin.)

However, there are many different tools out there for acceptance testing, they just haven’t been built specifically with WordPress in mind. And that is OK. Because when you are testing the front-end behavior of a site, it really doesn’t matter much what is powering the back-end. So a tool which can run tests on a scratch-built site will probably work very well for a WordPress site as well, or any other site.

Because WordPress is written in PHP, and tends to use PHPUnit for testing, Codeception is a natural choice for this. It is a framework that allows you to create unit tests (with PHPUnit), functional tests, and acceptance tests, which you can run with a single command. I think that perhaps this is just the kind of thing that you are looking for. If so, you will probably want to check out WP Browser as well, which is a set of WordPress-specific extensions for Codeception.

If there is an emerging go-to tool for acceptance testing WordPress plugins, I’d say that it might be Codeception. But to my knowledge, this is still a young thing among most WordPress developers, so we’ll have to wait and see what happens in the future. Don’t hesitate to jump in and start using it anyway though—maybe it will set a trend!


Update 2017-12-12

I’ve been using WP Browser for testing the WordPoints plugin, and I have been happy with it. I’ve also set up a repo demonstrating how to run WP Browser acceptance tests on Travis CI. There is still not a de facto standard in in the WordPress ecosystem, but Codeception + WP Browser remains one of the most prominent options.

Leave a Comment