Skip to main content

Getting Started with Drupal Contributions and Testing

Submitted by daniel on

Prior to Drupal 8, the Simpletest module was used to predominately run functional tests. What this meant was that tests were written in such a way that they test the interface as a whole instead of testing individual functions or finite pieces of code.

https://www.drupal.org/docs/7/testing/simpletest-testing-overview-drupal-7

When Drupal 8 was launched the use of Simpletest was demoted in favour of running phpunit tests. Php Unit has a rich pedigree as a testing framework tha has developed over a number of years and is influenced by other tried and tested Test Frameworks such as JUnit. As with other popular PHP Frameworks, it also brought Drupal inline with Robert C Martin's SOLID design principle's that encourage code to be understandable, flexible, and maintainable.

The goal of unit testing is to isolate each part of the program and show that the individual parts are correct. A unit test provides a strict, written contract that the piece of code must satisfy. 

https://en.wikipedia.org/wiki/Unit_testing#Benefits

In Drupal 8 the standard way to run the tests was by invoking Drupal core's run-tests.sh script.

In Drupal 9 support for Simpletest's were dropped in core with developers being encouraged to port there tests to phpunit. Also run-tests.sh, although still used on Drupal.org CI, was deprecated in favour of using PHP Unit and nightwatchjs.

Looking at how Continuous Integration is setup at drupal.org for testing, as well as we can see that before running any unit tests, typically we have the following that are run.

  1. phplint
  2. csslint
  3. eslint
  4. phpcs

Then the PHP Unit tests are run. These can be broken dopwn in to the following groups.

  1. unit
  2. kernel
  3. functional
  4. functional-javascript
  5. build

Note that simpletests, despite not being officially supported are still being run! 

It is still possible to run Simpletests via the now contributed SimpleTest module. What's interesting is this module started life as a contributed module, was adopted in core in Drupal 7, and then became available again as a contrib module in Drupal 8/9. 

In addition nightwatchjs is used to run all Drupal's end to end Nightwatch-based tests via yarn

So where to get started with testing in Drupal.org. There are quite a few obstacles to overcome such as setting up ChromeDriver etc for nightwatch, setting up node, yarn configuring your simpletest's etc. As you would expect there are various appoached to help get you started with running tests in drupal. 

One such way is to use the Drupal Contributions project that extends the lando project to make it relatively easy for you to get started with testing in Drupal locallty. After all this is suppossed to be a major benefit when usinga a modular PHP Framework like Drupal that also encourages well written code and adherrance to best practice's. The version or branch used will likely vary depending on what version of drupal you are using.

Here are a few useful commands to get you started:

List all tests groups:

lando phpunit --list-groups

Run one group of tests, for example BigPipe:

lando phpunit --group big_pipe

Run multiple groups of tests:

lando phpunit --group Group1,Group2

Exclude a group of tests:

lando phpunit --exclude-group Groupname

Run a single test from the BigPipe module:

lando phpunit web/core/modules/big_pipe/tests/src/Functional/BigPipeTest.php

https://github.com/lando/drupal-contributions

 

 

 

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.