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.
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 link
Acceptance Test for rmcl fails out of the screen. Adding
$I->click('Acceptance Test for rmcl'); solves it.
So the tests become:
$I->scrollTo(['css' => 'h2.entry-title']);
The Selenium server I used the last tutorial becomes buggy. I switched to use PhantomJS lately.
$ composer require --dev jakoch/phantomjs-installer neronmoon/scriptsdev site5/phantoman
Then, add this section in
PHPStorm (or your IDE) would complain about 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.
First, create a
codeception.dist.yml file so we can have different settings for different environments (local and Travis). Moving the settings in
codeception.dist.yml makes it easier to manage the settings. Here is all the settings we need for local and for TravisCI.
backup_globals: false # See: https://core.trac.wordpress.org/ticket/39327
Run the Tests Again
With the above configs, running
$ composer install or
$ composer update will install PhantomJS also.
$ composer install
- Installing phantomjs (2.1.1): Loading from cache
When running acceptance tests, PhantomJS starts and stops itself automatically.
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:
"rm -fr vendor",
"composer install --no-dev --prefer-dist --no-suggest",
"composer archive --format=zip --dir=release --file=remove-medium-cross-links"
$ 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.
Finally, TravisCI Settings
- php: nightly
- env: WP_VERSION=nightly
# Install helper scripts
- travis_retry composer global require --prefer-dist --no-suggest typisttech/travis-nginx-wordpress:"^2.0"
- export PATH=$HOME/.composer/vendor/bin:$PATH
# Build the production plugin
- travis_retry composer build
# Install the medium plugin
- wp plugin install medium --force --activate --path=/tmp/wordpress
# Activate the plugin
- wp plugin install ./release/remove-medium-cross-links.zip --force --activate --path=/tmp/wordpress
# Export a dump of plugin-activated database to the _data folder
- wp db export $TRAVIS_BUILD_DIR/tests/_data/dump.sql --path=/tmp/wordpress
- travis_retry composer install --prefer-dist --no-suggest
- vendor/bin/codecept run acceptance
Most of the work is done through the travis-nginx-wordpress package:
- Install Nginx
- Install correct version of WordPress core
- Ensure WordPress databases at localhost is ready for testing
- Start WordPress server at http://wp.dev:8080
Next, we build and install our plugin along with the official Medium plugin. Finally, install dependencies and actually run the tests.
Pushing to GitHub should trigger TravisCI to run the tests.
You can cache apt and composer packages to speed up the build. Make sure you add the
--prefer-dist flag in
$ composer install and add this section to
Travis Nginx WordPress
Until Next Time…
Full source code is can be found on GitHub.
Nothing stops our journey to TravisCI. I will talk about how to publish the plugin to wordpress.org after tests passed. Make sure you have subscribed my newsletter!
Originally published at Typist Tech.