Testing with PHPUnit in Wercker

Wercker is a Docker-Native CI/CD Automation platform for Kubernetes & Microservice Deployments

Zachary Flower
Zachary Flower
May 15, 2017

Unit testing isn't optional anymore. To be honest, I can't think of a time when it was optional. But as the DevOps community has grown, so has the emphasis on proper software development practices: automated build processes, atomic deployments, environmental parity—all things that a high quality CI/CD platform like Wercker offers.As a PHP developer, PHPUnit is the unit testing framework that I typically use.

Thankfully, due to the flexibility of the Wercker platform, configuring and running PHPUnit tests is incredibly straightforward. I’ll show you how in this post.


Getting started

Before we get into running a PHPUnit test suite with Wercker, though, let's first spin up a simple PHP application and go over some PHPUnit basics. To get started, create a new project with the following directory structure:




Then, create a file called Hello.php in the src/ directory. This will be the source code that we will run our test suite against. For the sake of this demonstration, the contents of this file should look like this:


<?php namespace Hello;

class Hello {
 static public function world() {
   return "Hello world!";



Once we've created our source code, we next need to set up a composer.json file. Composer is the standard package management tool for PHP applications. While most applications have many more dependencies than this demo, all that we need to require is the PHPUnit package itself.


 "name": "hello/world",
 "autoload": {
   "psr-4": {
       "Hello\\": "src/"
 "require": {
   "php": ">= 5.3.0"
 "require-dev": {
   "phpunit/phpunit": "~4.2"


After setting up our composer.json, we then need to install all the required dependencies. This is accomplished by running the composer install command. In addition to installing dependencies, this command will set up an autoloader file which can be imported into a PHP script to autoload all of the dependencies.


composer install


With PHPUnit successfully installed, it's now time to configure it. PHPUnit expects a phpunit.xml configuration file at the project root, which tells it exactly where to find the unit tests, and how to run them. To keep things simple, we'll tell PHPUnit to run all .php files in the tests/ directory, and to import the Composer autoloader (vendor/autoload.php) before running the suite.


<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
       <testsuite name="Package Test Suite">
           <directory suffix=".php">./tests/</directory>


The final step toward testing our project source code is to actually write some tests. Since there's not a whole lot to test here, we can demonstrate this with a single assertion in the tests/testHello.php test file.



use Hello\Hello;

class HelloTest extends PHPUnit_Framework_TestCase {
 public function testSuccess() {
   $this->assertEquals('Hello world!', Hello::world());



And that's it! With our handy-dandy new test suite in hand, all we have to do to execute it is run the vendor/bin/phpunit command. If everything goes right, you should see a result like this:


$ vendor/bin/phpunit

PHPUnit 4.8.35 by Sebastian Bergmann and contributors.


Time: 91 ms, Memory: 4.00MB

OK (1 test, 1 assertion)


PHPUnit + Wercker

Now that we have a PHP application and a fully functioning test suite, it's time to integrate it into Wercker. But before we can do anything, we first need to create a wercker.yml file.


box: php
   - install-packages:
       packages: git
   - script:
       name: install composer
       code: curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
   - script:
       name: install dependencies
       code: composer install --no-interaction
   - script:
       name: PHPUnit integration tests
       code: vendor/bin/phpunit


This file, placed at the root of our project directory, tells Wercker exactly how to configure and run our application's test suite. As you can see, the same steps we take to manually configure our testing environment are configured to run serially within the Wercker config.

Once we've set up our Wercker config and linked our project's version control repository to our Wercker account, every time we push up new code, our test suite will run automatically. The output will look something like this:


Wercker steps


Taking it further

While a simplistic example by nature, the power of automated PHPUnit testing with Wercker should be readily apparent. Beyond simply running integration and unit tests for a PHP application, the same workflow can be augmented to run tests for multiple languages within the same project, as well as initiate browser-based testing. If the process can be done manually, it can also be done automatically.


About the Author

Zachary Flower is a freelance web developer, writer, and polymath. He has an eye for simplicity and usability, and strives to build products with both the end user and business goals in mind. From building projects for the NSA to creating features for companies like Name.com and Buffer, Zach has always taken a strong stand against needlessly reinventing the wheel, often advocating for the use of well established third-party and open source services and solutions to improve the efficiency and reliability of a development project.


Like Wercker?

We’re hiring! Check out the careers page for open positions in Amsterdam, London and San Francisco.

As usual, if you want to stay in the loop follow us on twitter @wercker or hop on our public slack channel. If it’s your first time using Wercker, be sure to tweet out your #greenbuilds, and we’ll send you some swag!


Topics: Tutorials