Skip to main content

Troubleshooting Drupal 8 Kernel Tests

Back-end Development
Drupal

I don't have a lot of experience writing new automated tests for custom Drupal modules. And so, perhaps expectedly, once I decided to dive in, I encountered a number of problems along the way.

If you are interested in learning a bit more about the whole process of writing Drupal 8 Kernel tests, see my other post here.  It's also worth noting a number of different errors can be a result of not correctly identifying your `phpunit.xml` file. Make sure you have this file, and are correctly identifying its location with the --configuration cli option.  Also be sure to use the Drupal phpunit.xml file as a starting point, which can be found at core/phpunit.xml.

 

PDOException: Base table or view not found

You may see something like the following.

There was 1 error:

1) Drupal\Tests\mymodule\Kernel\AuthorizationServiceTest::testSuccessfulLogin
Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mysite.test20951378key_value_expire' doesn't exist: SELECT name, value FROM {key_value_expire} WHERE expire > :now AND name IN ( :keys__0 ) AND collection = :collection; Array
(
    [:now] => 1588704859
    [:collection] => user.private_tempstore.mymodule
    [:keys__0] => HSmqBoYuyRW4B_bMnh2JoeHftpOGVpinMJ3PlbmifNk:crmToken
)
Caused by
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mysite.test20951378key_value_expire' doesn't exist

This indicates that you need to include schema in your test. In the setUp() method you can include schemas like the following.

/**
 * {@inheritdoc}
 */
protected function setUp() {
  parent::setUp();
  $this->installSchema('system', ['key_value_expire']);
}

Finding the particular schema needed can be a little difficult if it's not something you have included before. The easiest way to discover the proper way to include the necessary schema is to search the Drupal code base for the table name indicated in the error. The table name will be listed something like mysite.test20951378key_value_expire.  You want the portion following the test hash.

Missing environment variables

Exception: There is no database connection so no tests can be run. You must provide a SIMPLETEST_DB environment variable to run PHPUnit based functional tests outside of run-tests.sh. See https://www.drupal.org/node/2116263#skipped-tests for more information.

This particular error contains helpful information. It indicates that the database connection can not be made without the specified environment variable. This environment variable and others can also be specified directly in your phpunit.xml file. Depending on the test being run, variables like BROWSERTEST_OUTPUT_DIRECTORY and SIMPLETEST_BASE_URL may also be required. 

One approach to setting these variables is to do it via shell script, and then include it when executing the test.

source setup-testing.sh && vendor/bin/phpunit --configuration web/sites/default web/modules/custom/mymodule/tests/src/Kernel/AuthorizationServiceTest.php

 

Module(s) not available

There was 1 error:

1) Drupal\Tests\mymodule\Kernel\AuthorizationServiceTest::testSuccessfulLogin
LogicException: system module is not enabled.

This indicates that you need to include a module in the $modules constant defined in your Kernel test. It's common for some modules to be indirectly needed. 

/**
 * The modules to load to run the test.
 *
 * @var array
 */
public static $modules = [
  'user',
  'profile',
  'externalauth',
  'system',
  'field',
  'text',
  'views',
];

 

Missing Schema

There was 1 error:

1) Drupal\Tests\mymodule\Kernel\AuthorizationServiceTest::testSuccessfulLogin
Drupal\Core\Config\Schema\SchemaIncompleteException: No schema for views.view.profiles

This one likely comes from one of two scenarios:

  • You need to include a module in your $modules class constant. In this case the views module.
  • You must install a particular schema in your setUp() method. 

Base class not found

PHP Fatal error:  Uncaught Error: Class 'Drupal\KernelTests\KernelTestBase' not found in /var/www/html/web/modules/custom/mymodule/tests/src/Kernel/AuthorizationServiceTest.php:15

This error usually indicates that you have not correctly specified the location of your phpunit.xml file. By default, PHPUnit assumes the phpunit.xml file is in the same directory you are executing the command in. If you have it someplace else, like your sites directory, it must be specified with the --configuration option.

vendor/bin/phpunit --configuration web/sites/default web/modules/custom/mymodule/tests/src/Kernel/AuthorizationServiceTest.php

It is also possible that you have correctly identified your phpunit.xml file, but it doesn't contain all the correct information. Drupal comes with a sample version core/phpunit.xml.dist. Make sure your file contains the required testsuites.

Need a fresh perspective on a tough project?

Let’s talk about how RDG can help.

Contact Us