BS Blog

Running PHPUnit Tests in Docksal

Published Feb 15, 2021 | 2 Comments

Running PHPUnit tests is essential to Drupal development, especially when contributing to reputable modules or Drupal Core; however, sometimes this is easier said than done.

I typically run Docksal on my local environment, but this likely can be applied to Lando, DDEV, and any other docker-compose setup.

First thing you'll need is a browser, luckily Docksal provides a nice container for this:

yaml
services: # Browser browser: hostname: browser image: selenium/standalone-chrome dns: - ${DOCKSAL_DNS1} - ${DOCKSAL_DNS2}

Add that to your .docksal-local.yml and run fin up to activate.

IMPORTANT!

You also need to be sure your project actually has PHPUnit and it's dependencies, to do this run composer require --dev drupal/core-dev -W

Read more about that here

Next you'll want to copy over <docroot>/core/phpunit.xml.dist over to phpunit.xml (I just left it in the core dir in this case)

You will need to update some values here to match your setup. Since I am using Docksal the main changes are here. you'll want to adjust for your specific setup:

xml
<env name="SIMPLETEST_BASE_URL" value="http://web"/> <env name="SIMPLETEST_DB" value="mysql://user:user@db/default"/> <env name="MINK_DRIVER_ARGS_WEBDRIVER" value='["chrome", { "chromeOptions": { "w3c": false } }, "http://browser:4444/wd/hub"]'/>

Now we can attempt to run our first test, I'm going to run core's Help module tests:

phpunitresult

And that's it that's all, now get out there and write some tests you lil scamp!