Skip to main content

Running Drupal's PHP Unit tests with Headless Chrome Image and ddev

In a previous article I have looked at running Drupal's Nightwatch and PHPUnit Tests using Drupal's Quickstart development environment. Today, I will look at how to run all the the PHP Unit Tests using a headless version of chrome.

Setting up Headless Chrome Image

Within your project root within the .ddev folder create a new YAML file and call it docker-compose.chromedriver.yaml

version: '3.6'
    container_name: ddev-${DDEV_SITENAME}-chromedriver
    image: drupalci/chromedriver:production
    shm_size: '1gb'
        soft: -1
        hard: -1
      - "4444:4444"
      - "9515:9515"
      - chromedriver
      - "--log-path=/tmp/chromedriver.log"
      - "--verbose"
      - "--allowed-ips="
      - "--allowed-origins=*"
    # These labels ensure this service is discoverable by ddev ${DDEV_SITENAME}
      com.ddev.approot: $DDEV_APPROOT ${DDEV_PRIMARY_URL}
  # This links the Chromedriver service to the web service defined
  # in the main docker-compose.yml, allowing applications running
  # in the web service to access the driver at `chromedriver`.
      - chromedriver:$DDEV_HOSTNAME

Then you will need to spin up your environment again to install and load the new chromedriver image e.g. ddev start

Next we will need to copy your core/phpunit.xml.dist to core/phpunit.xml and modify the following:


<!-- Example SIMPLETEST_BASE_URL value: http://localhost -->
<!-- Changed to http://web to enable Chrome Headless Browser access to base url from ddev container -->
<env name="SIMPLETEST_BASE_URL" value="http://web"/>


<!-- Example SIMPLETEST_DB value: mysql://username:password@localhost/databasename#table_prefix -->
<!-- Changed to mysql://db:db@db/db for default ddev access use 'ddev describe' to check your credentials -->
<env name="SIMPLETEST_DB" value="mysql://db:db@db/db"/>


<!-- Parameters pass to Chromedriver. -->
<!-- Example for changing the driver args to webdriver tests MINK_DRIVER_ARGS_WEBDRIVER value: '["chrome", { "chromeOptions": { "w3c": false } }, "http://localhost:4444/wd/hub"]' For using the Firefox browser, replace "chrome" with "firefox" -->
<env name="MINK_DRIVER_ARGS_WEBDRIVER" value='["chrome", {"browserName":"chrome","chromeOptions": { "w3c": false,"args":["--disable-gpu","--headless", "--no-sandbox"] } }, "http://chromedriver:9515"]'/>

Running the tests

If all goes well well we should now be able to run the PHP Unit tests in headless mode. Let's test this.

  1. Make sure that you have installed your dev dependencies e.g. composer install
  2. log into your container e.g. ddev ssh 
  3. from /var/www/html/web run ../vendor/bin/phpunit -c core --list-groups . The -c flag specifies where to load the config from e.g. the core directory. With a bit of luck we can see all the test groups listed.
  4. If we just want to run a single group e.g the migrate we can run like so ../vendor/bin/phpunit -c core --group migrate
  5. If we want to run a specific test we can also do like this ./vendor/bin/phpunit -v -c core core/modules/action should now output some thing like:
/var/www/html/web$ ../vendor/bin/phpunit -c 
core core/modules/action
PHPUnit 9.6.13 by Sebastian Bergmann and contributors.

Testing /var/www/html/web/core/modules/action
......                                                              6 / 6 (100%)

Time: 00:22.318, Memory: 6.00 MB

OK (6 tests, 62 assertions)

Testing a contributed module

../vendor/bin/phpunit -c core modules/contrib/migrate_plus


If you want to check debug and check that chromedriver is running the following command might be helpful:

docker exec -it ddev-{YOUR_PROJECT_NAME}-chromedriver tail -f /tmp/chromedriver.log

Further reading/viewing


Getting up and running locally with running the tests:

Writing some tests


Add new comment

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.