Adding Unit tests to a WordPress plugin via VVV and PHPUnit

While the official documentation can be found at , this is very far from being approachable to folks who are just starting (like me). It honestly took half a dozen of tries during the last year to make it work as expected.

While steps 1 ( Install PHPUnit ), and 2 ( Install WP-CLI ) are somewhat clear, step 3 ( Use WP-CLI to setup our plugin’s unit tests ) becomes extremely muddy very fast.

This has been haunting me for a while, as every time I have tried to make it work in the past it failed sooner or later through the process. Finally using VVV (as well as being more acquainted with solving the errors I was finding along the way) I was able to make it work, not without a full day of hiccups though.

Here’s a list of problems and its solutions I faced when making VVV and PHPUnit work for testing WordPress plugins. At the moment is pretty dry but I’ll try to keep updating it as I encounter new ones 🙂

This is based on this tutorial , which is the best I found so far.

Problem: Need to install PHPUnit via composer, but .phar instructions are given.


Problem: WP-CLI and wp scaffold plugin-tests your-plugin-name does not work

Solution: Needs to be run inside the virtual machine, not in the host machine (outside).

Problem: Error establishing a database connection

Solution: When running the PHPUnit tests, WordPress will load the config from wp-tests-config.php instead of wp-config.php. You need to add your database connection details in wp-tests-config.php

Problem: mysqli_real_connect(): (HY000/2002): No such file or directory in …

Solution: Same as “Error establishing a database connection”

Problem: MYSQL . CREATE DATABASE failed; error: ‘Access denied for user … ‘

Solution: Your user needs proper privileges:

Problem: PHPUnit returns “No tests executed”

Solution: By default phpunit.xml.dist will exclude the sample test.

Problem: Could not find /tmp/wordpress-tests-lib/includes/functions.php


If is the first time:

If is not the first time:

Error: Massive file changes in GIT but only showing old mode vs new mode:


And works! Just needed to jump through 16 loops on fire 😀


Leave a Reply