Hero Image

Three Step Functional Testing With Lando and Drupal

In order to submit patches to the Drupal community and have them accepted, it is advised that your patches include a test coverage. A test coverage acts as proof that the code you’re submitting has been tested and works as expected. If future changes were to happen to the codebase that breaks existing code, your test coverage eliminates or highlights where the issue is coming from. 

This article touches on a form of testing you can submit to the Drupal community, which is functional testing. In regards to functional testing, Drupal 8 ships two base classes which we can extend i.e. BrowserTestBase and WebDriverTestBase. 

The difference between the two types is that WebDriverTestBase works with javascript enabled. This article assumes you have Lando setup locally and a connection to a drupal 8/9 site. Lando is an abstraction layer and superset of Docker Compose. It is used for mimicking production environments locally.

Step 1: Make Modifications

The first thing you need to do is make modifications to your .lando.yml file. Under the services section, you will need to add some overrides to your environment so that your environment is set up to test properly. These are the same overrides you would normally do to your phpunit.xml file to create environment variables.

Screenshot of modifying the .lando.yml file.

Step 2: Add Docker Compose for Selenium

The next thing we want to do is add a docker compose for Selenium. Selenium is a web driver used for simulating browser tests. Adding a Docker Compose to Lando creates Selenium in a Docker container to run tests on. 

With Lando, it is possible to create a custom service using Docker Compose by adding ‘type: compose” in your Lando file. The Docker Compose contains an image which will be pulled i.e. selenium/standalone-chrome-debug and the ports that will be used for the connection i.e. 4444 and 5900. 

Note here, port 5900 is used to view the test inside the container. This will be explained later on.

Screenshot of port 5900 that is used to view the test inside the container.

Step 3: Rebuild & Test Your Local Environment

After adding the code from the image above under the services section, you would need to rebuild your local environment so that these changes take effect. To do so, run the command Lando rebuild. 

The next thing we would need to do is check to see if our test setup works. To do so, we would need to set up a configuration file. Luckily, Drupal core comes with its own configuration file for phpunit called phpunit.xml.dist. If you would like to modify this, you can copy it’s content into a phpunit.xml file of your own and set our phpunit to point to that configuration file. 

In my case, I added testsuite called ‘custom unit’ in my phpunit.xml file and inside my .lando.yml file, and added a command to tooling called ‘phpunit’ which executes custom unit tests. When I run Lando phpunit, it runs the tests in the customunit section.

Screenshot of command to tooling called ‘phpunit’ which executes custom unit tests.

In order to see the tests in action, you would need to download a VNC viewer.. Once downloaded and installed. Open VNC viewer and type in the address localhost:5900, this will open up a prompt to type in a username and password. 

Leave the username blank and type in ‘secret’ for password and you will be connected to your selenium container to see tests being executed.

Screenshot of login for a VNC viewer.

Contributing to Drupal Helps All of Us

As an active Drupal community contributor, we pride ourselves in supporting our developers to actively contribute to the Drupal software platform, volunteering hundreds of development hours each year to make it better for everyone. If you are ready to join in, we encourage you to get involved! 

The community is working on a brand new Contributor Guide to help you 
figure out how to use your particular skills to contribute to the Drupal project and add your story to the Drupal community. 

 

Henry Odiete

Developer

Drupal Certified Front End Specialist with 3+ years experience in web design and development. Henry enjoys learning new technologies and applying them in his work.

Related

Why Drupal Matters

Why enterprise adoption of one of the world’s largest open-source projects is more important today than ever....

Ottawa

135 Laurier Ave. West, Suite 100
Ottawa, ON K1P 5J2
TEL: 1 (877) 257-6746

Saratoga Springs

PO Box #817
Saratoga Springs, NY 12866
TEL: 1 (877) 257-6746