Running WordPress Plugin Acceptance Tests on TravisCI

We have written come acceptance tests in the last tutorial. Time to run them on the cloud. No way I would spend 10 minutes to wait for my tests ran for multiple PHP and WordPress versions.

Continue from the last tutorial, I’ve removed some unused pieces and added PHP_CodeSniffer to the project. Nothing related to acceptance testing. Head over to the GitHub repo if you are interested.

Prevent Race Conditions

A problem I found is that the tests failed randomly. The reason is that our tests tries to click some links before they show up on the screen. Adding $I->waitForElement('some element', 10); solves the problem.

Scroll To the Link

Secondly, when changing the screen size, the linkAcceptance Test for rmcl fails out of the screen. Adding$I->click('Acceptance Test for rmcl'); solves it.

PhpStorm (or your IDE) would complain scripts-dev is not valid in composer.json. It’s totally fine because we only want to install PhantomJS on dev environment. I’ve made a pull request to move it into the extra section. Until it’s merged, ignore the warning.

Codeception Config

First, create a codeception.dist.yml file so we can have different settings for different environments (local and Travis). Moving the settings in acceptance.suite.yml into codeception.dist.yml makes it easier to manage the settings. Here is all the settings we need for local and for TravisCI.

Building the Plugin

We need a way to install our plugin on travis. Using composer archive to generate a zip file and then install it via WP-CLI seems the simplest solution. Adding a composer build command into composer.json:

Now, run $ composer build to make the zip file. This is the final plugin you want to publish on wordpress.org. Note that we don’t need vendor in this simple plugin, but it’s required in most of the cases. You can also add more tasks in the build process, for example: grunt addtextdomain and grunt makepot.