Drupal 8 Automated Testing with Travis CI

Travis CI burst onto the hosted continous integration scene by offering free testing for public projects. If your code is on Github and available to the public then Travis will run your tests for you.

I will show you how to get Drupal 8 running all of it's PHPunit tests in Travis. If you want to use Travis for your own private repo, you will have to pay a little bit. In my opinion that is a small price to pay for never setting up Jenkins again.

First we specify our programming language and the version.

language: php

  - 5.4

Next up we specify our database information. Note that we do not have to specify install steps for either PHP or MySQL.

  database: drupal 
  username: root 
  encoding: utf8

Travis provides different steps which are run prior to building the environment. In our case we make sure composer is up to date and make sure our package manager is up to date in case we need to add any additional packages to our environments in the install step that follows.

  - sudo apt-get update & /dev/null 
  - composer self-update

Now we do the actual install, we ensure composer is set up properly to support the composer Drush install and then 'rehash' our PHP environment to ensure that composer is available for our Drush install.

  # add composer's global bin directory to the path
  # see: https://github.com/drush-ops/drush#install---composer
  - export PATH="$HOME/.composer/vendor/bin:$PATH"

  # Install Drush
  - composer global require drush/drush:dev-master
  - phpenv rehash

Still in the install step we continue by making sure our database exists and then use our newly installed Drush to run our default site install.

  # Create MySQL Database
  - mysql -e 'create database drupal;'

  # Install drupal default profile
  - drush --verbose site-install --db-url=mysql://root:@ --yes

With an installed copy of Drupal we can now import our default active config to create a site that is similar to our production site just without the content.

  # Import config for our pseudo install
  - drush --verbose config-import active --yes

Finally we need to be running from the core directory for Travis to find Core's PHPunit tests and Travis will run them automagically.

  # Move into core directory for phpunits benefit
  - cd core

The complete example from above is available at: https://github.com/mrf/drupal/blob/8.0.x/.travis.yml

We now have a pseudo-copy of our site and can run core's default tests on it. This is nice for a quick sanity check of your site to make sure you didn't break anything horribly.

This is just a basic intro to what is possible with Travis. Travis provides the ability to run Behat and PhantomJS tests as well as many other testing frameworks for other languages and platforms. Using this example as a starting point you should be able to test almost any aspects of your Drupal 8 site without having to maintain your own testing infrastructure.

This example reperesents my first serious attempt at using Travis for running tests. In order to piece together how everything worked together I stood on the shoulders of giants and learned a lot from the following resources.`