Running plugin unit tests vs. integration tests?

As you’ve found there’s a whole raft of outdated, poor and convoluted information around unit testing in WP currently. However Josh Pollock, a well respected WordPress dev and Core contributor did a YouTube series on testing in WordPress least year which I found very helpful.

As I’ve been on the unit testing in WordPress journey too, and here is a high level rundown of what I’ve found:

  • As you may know phpUnit is the defacto PHP testing framework, and the majority of of the framework agnostic packages and tooling for PHP are somehow based upon it.

  • Due to the nature of WordPress it is difficult not to call global functions from your code, which of course are not available without either spooling up an instance of WP or mocking them somehow. Further, the WP docs offer no discussion on different approaches, which is likely why most tutorials start by using the wp_cli to scaffold out the tests. I for one have that found the wp_cli
    approach to not be very well documented, and very slow in practice, as an instance of WP and its database connection was spooled up for each test, which made the test quite slow. This was my experience anyway, and there may be ways to mitigate this. The WP approach also requires a separate DB to interact with your tests, as you want to be mindful not to accidentally corrupt any real data during a failed test.

  • To avoid all this, you could write mocks for WP functions yourself, which is possible in simple use cases, however these quickly become tedious write and maintain for projects with any complexity. There two libraries that are often cited for this purpose:

Of the two, WP_Mock has more GitHub stars, more recent commits, and is actively maintained by GoDaddy and 10up. By comparison Brain Monkey by Giuseppe Mazzapica, is also well reviewed, and Josh describes it as ‘excellent’ in his tutorial series. Brain Monkey however has seen less community adoption (based on stars), and no significant contributions in recent years. This may be because it is largely feature complete –– however being maintained by a solo dev is likely a contributing factor. In fairness to Giuseppe, he has been active on any in issues posted in the past year.

Another project to consider is Codeception, which describes itself as follows:

Codeception collects and shares best practices and solutions for testing PHP web applications. With a flexible set of included modules tests are easy to write, easy to use and easy to maintain.

Codeception can be thought of as a superset of PHPUnit, and according to the docs phpunit and Codeception test are almost entirely interoperable. Codeception supports a range of testing approaches including functional, unit and acceptance tests. This creates a happy path as your needs grow without having to worry about configuring something like Selenium. Finally, in addition to first class WordPress support, it also also supports Symphony, Laravel and others. Codeception’s versatility and broad support make it a very attractive proposition.

For many projects Codeception is surely overkill, however, if you also use other frameworks your experience will be transferable. This said, I have seen reviewers who observe that Codeception’s yml configuration is cumbersome to set up and difficult to learn – these reviews are dated however and may not reflect the current state of the framework.

Personally, Im just now exploring WP_Mock for my project, as of the three bears, it feels like it may be ‘just right’.