Skip to main content

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

Submitted by daniel on

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'
services:
  chromedriver:
    container_name: ddev-${DDEV_SITENAME}-chromedriver
    image: drupalci/chromedriver:production
    shm_size: '1gb'
    ulimits:
      core:
        soft: -1
        hard: -1
    ports:
      - "4444:4444"
      - "9515:9515"
    entrypoint:
      - chromedriver
      - "--log-path=/tmp/chromedriver.log"
      - "--verbose"
      - "--allowed-ips="
      - "--allowed-origins=*"
    labels:
    # These labels ensure this service is discoverable by ddev
      com.ddev.site-name: ${DDEV_SITENAME}
      com.ddev.approot: $DDEV_APPROOT
      com.ddev.app-url: ${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`.
  web:
    links:
      - 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:

SIMPLETEST_BASE_URL

<!-- 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"/>

SIMPLETEST_DB

<!-- 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"/>

MINK_DRIVER_ARGS_WEBDRIVER

<!-- 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

Debugging

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

See https://mglaman.dev/tags/testing

Getting up and running locally with running the tests: https://www.youtube.com/watch?v=6TkuacrAHX8

Writing some tests https://www.youtube.com/watch?v=HFIpMwSs3y4

 

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.