Tutorial: How to test your WordPress plugin with Bitbucket pipelines

When several developers are working together, it is hard to keep an eye on the PHPUnit tests results. Because you depend on the last person’s tests. To solve this issue, we required a platform where we can see the latest test results. Within AppSaloon we’re using the Bitbucket pipelines to do that, but it shows more than the latest result, which is even better.

Before reading further, I’ll assume that you know how to write a PHPUnit test, because in this post we will not go into that. It will be more about how to run those tests in Bitbucket pipelines.

The first step is to create a new file in your repository “bitbucket-pipelines.yml“. In this file, we will configure the server setup in Bitbucket pipelines.

# This is a sample build configuration for PHP.
# Check our guides at https://confluence.atlassian.com/x/e8YWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
pipelines:
  default:
    - step:
        image: tfirdaus/wp-docklines:php7.2-fpm-alpine
        name: "PHP 7.2"
        script:
          ## Run PHPUnit
          - bash bin/install-wp-tests.sh wordpress_tests wp password 127.0.0.1 latest true
          - phpunit
        services:
          - database

definitions:
  services:
    database:
      image: mysql:5.7
      environment:
        MYSQL_DATABASE: 'wordpress_tests'
        MYSQL_USER: 'wp'
        MYSQL_PASSWORD: 'password'

The image(tfirdaus/wp-docklines:php7.2-fpm-alpine) is a Docker image with the following features included:

  • WordPress
  • WP-CLI
  • PHPUnit
  • PHPCS with WordPress Coding Standards
  • Subversion
  • Git

You can find more versions here.

The next line is to generate the test files for the PHPUnit:

bash bin/install-wp-tests.sh wordpress_tests wp password 127.0.0.1 latest true

After that we run the PHPUnit.

phpunit

For PHPUnit testing, we also need to configure a database service. The credentials need to be the same in phpunit.xml.

definitions:
  services:
    database:
      image: mysql:5.7
      environment:
        MYSQL_DATABASE: 'wordpress_tests'
        MYSQL_USER: 'wp'
        MYSQL_PASSWORD: 'password'

After every commit, the pipelines will run the tests.

When it fails, the admin will get notified by e-mail, mentioning something went wrong.

Conclusion

Bitbucket pipelines can be used for more than one goal. In our issue, we used it as a testing platform for our plugin. That way, we can develop our plugin with a team of developers and still track the unit test results.