Enabling Simpletest

Before we can do anything with tests we need to enable the simpletest module. You can do that from the Backdrop UI at /admin/modules/list or with drush. Let's use drush as writing, running, and iterating on testing cycles can be sped up by using the command line rather than the UI.

Here is how to enable simpletest with drush on the command line:


lando drush en simpletest


To check if simpletest is enabled we can use drush pml command.



lando drush pml |grep simpletest

Running Simpletests

The fastest way to iterate with testing is via the command line. Backdrop provides a run-tests.sh bash script that we can use to run tests. To figure out how to use it invoke the --help like so:

./core/scripts/run-tests.sh --help

Here is what the output of run-tests.sh --help looks like:


Run Backdrop tests from the shell.

Usage:        run-tests.sh [OPTIONS] 
Example:      run-tests.sh --url="http://backdrop" BootstrapPageCacheTestCase

All arguments are long options.

  --help      Print this page.

  --list      Display all available test groups.

  --clean     Cleans up database tables or directories from previous, failed,
              tests and then exits (no tests are run).

  --url       Immediately precedes a URL to set the host and path. You will
              need this parameter if Backdrop is in a subdirectory on your
              localhost and you have not set $base_url in settings.php. Tests
              can be run under SSL by including https:// in the URL.

  --php       The absolute path to the PHP executable. Usually not needed.

  --concurrency [num]

              Run tests in parallel, up to [num] tests at a time.

  --force     Enable the Simpletest module if it's not enabled already.

  --all       Run all available tests.

  --group     Run tests identified by specific group names, instead of class names.

  --file      Run tests identified by specific file names, instead of class names.
              Specify the path and the extension
              (i.e. 'core/modules/user/user.test').

  --directory 

              Run all tests found within the specified file directory.

  --xml       

              If provided, test results will be written as xml files to this path.

  --color     Output text format results with color highlighting.

  --verbose   Output detailed assertion messages in addition to summary.

  --cache     Generate cache for instalation profiles to boost tests speed.

  --summary [file]

              Output errors and exception messages to summary file.


  [ [  ...]]

              One or more tests classes (or groups names) to be run. Names may
              be separated by spaces or commas.

To run this script you will normally invoke it from the root directory of your
Backdrop installation as the webserver user (differs per configuration), or root:

sudo -u [wwwrun|www-data|etc ./core/scripts/run-tests.sh
  --url http://example.com/ --all
sudo -u [wwwrun|www-data|etc] ./core/scripts/run-tests.sh
  --url http://example.com/ BlockTestCase


If you are working directly on your local or server this will work for you, but since we are working in lando we can integrate this script as part of our tooling. I'll add a test key to the tooling setion of my .lando.yml file.


tooling:
  test:
    service: appserver
    cmd:
      - /app/core/scripts/run-tests.sh --url https://backdrop.lndo.site --color --verbose


Now we can get the same help with:


lando test --help

and we've also piped into our command some nice flags like --url, --color, and --verbose

We can run all the tests like this:


lando test --all

but often it is more convenient to hone in on the tests we are currently writing. the run-scripts.sh allows us to run the tests from one directory or by naming a test class like BlockTestCase we can see all that explained in the output of lando test --help. Here is how to run just the tests in the BlockTestCase:


lando test BlockTestCase

and here is what the output looks like:


Block: Block functionality (BlockTestCase) 133 passes, 0 fails, 0 exceptions, and 41 debug messages. [23.176s]

Backdrop test run
---------------

Tests to be run:
 - Block functionality (BlockTestCase)

Test run started:
 Friday, April 10, 2020 - 12:33am

Test summary
------------


Test run duration: 23 sec

Detailed test results
---------------------