November 17, 2016
Automate your Drupal 8 project testing with Travis CI
Travis CI is a hosted continuous integration service that offers free testing for public github repositories which makes it a great candidate to integrate with your next Drupal project.
I recently set up CI for a project I’m maintaining and found that it wasn’t as easy as I thought. especially when attempting to use Drupal 8’s functional testing framework.
Setup
Travis CI allows you to commit a configuration file which will describe how your testing environment should be built. We’ll run through a sample .travis.yml
file and see what it’s doing.
First you need to define your language and versions.
This will tell Travis CI to build 2 environments (5.6 and 7) and perform your tests on both.
Next, you should add Composer’s vendor directory to the path so we can use Drush.
Travis will create new environments for each line in your environment variable section, this lets you tests a number of configurations. To define multiple environment variables for a single environment you separate them with a space.
Then you define MySQL credentials just defining credentials is enough to tell Travis that it needs to install MySQL.
Travis provides a number of ways to customise the build prior to running the tests. As we’re going to be using Drush to install Drupal, this is a great place for ensuring it is available.
Now we can define the install steps for setting up Drupal.
By using composer to create a new Drupal project we get all the dev dependencies of the project. This is necessary as we need to use the bundled version of PHP Unit and not Travis’ version.
After we download Drupal and set up MySQL we need to install Drupal and move our project into the correct directory (modules
for modules and themes
for themes). This should still be in the install
key of your YML file.
When running functional tests we need to have a server available that can handle requests this is why we run drush rs 8080 -
we sleep the process to ensure the server has enough time to start.
We now have a PHP environment running the latest stable version of Drupal 8 and our project. The last thing we need to do is run the tests.
The mink driver for the BrowserTestBase
class still relies on some SIMPLETEST_
environment variables. You can specify those prior to running the PHP Unit binary which will override the defaults provided by the configuration file we pass in.
See a full example.
Gotchas
- Travis lets you run commands like
phpunit
with a pre-installed version this is not compatible withBrowserTestBase
- Using an older version of PHP Unit means that the latest documentation is not available
- If you are only running unit tests you don’t need to run a server
Useful resources
This was the first attempt I made a running functional PHPUnit tests on Travis. Here are some articles that I found very helpful in getting this all set up.
- https://docs.travis-ci.com/
- https://www.drupal.org/docs/8/phpunit https://www.chapterthree.com/blog/drupal-8-automated-testing-travis-ci
- https://github.com/LionsAd/drupal_ti
- https://www.drupalwatchdog.com/blog/2014/12/test-now-travis-integration-your-drupal-modules
- http://blog.freelygive.org.uk/2016/01/15/testing-with-travis-ci-on-github/