Two Beginner Errors In Drupal 6 Simpletest

7th October 2010

Simpletest is a Drupal module that I have been using for about a week to unit test a module I have been working on. It is a brilliant and powerful module that every module developer should be using to test their code properly. I did, however, encounter a couple of wierd errors that took me a while to fix. So for the benefit of everyone else I thought I would post them here, along with the solutions. These errors are probably things that developers new to Drupal Simpletest will come across, which is why I have labelled them beginner errors.

The first is definitely an error that wouldn't be immediately obvious to everyone who has just started to use the module. What happens is that you will try to run some tests and you will see "table doesn't exist" errors appearing when you try to do anything with the core database tables or even tables outside of your module. After a good hour spent going through all of the Drupal Simpletest documentation on the drupal.org site I wasn't any clearer as to what was going on.

The reason this error is happening is because when creating Drupal Simpletest test classes you will extend one of two parent classes; these are DrupalWebTestCase and DrupalUnitTestCase. The reason that table errors are occurring is because you are probably using the DrupalUnitTestCase class to do things with core tables. A quick look at the definition for the DrupalUnitTestCase class reveals what is going on.

  1. /**
  2.  * Test case for Drupal unit tests.
  3.  *
  4.  * These tests can not access the database nor files. Calling any Drupal
  5.  * function that needs the database will throw exceptions. These include
  6.  * watchdog(), function_exists(), module_implements(),
  7.  * module_invoke_all() etc.
  8.  */
  9. class DrupalUnitTestCase extends DrupalTestCase {

It is clear from this comment (and the constructor) that no database interaction is going on when this class initialises. The test class you have written is trying to access test tables that haven't been set up, changing the class definition to extend DrupalWebTestCase (as below) will solve this issue.

  1. class MyAmazingModule_Test extends DrupalWebTestCase {

The reason for this is that the DrupalUnitTestCase class should only be used to test things that don't need database or file level access. These are things like string manipulation or calculations. The DrupalWebTestCase class should be used when testing everything else.

The second issue I had was that whenever I tried to run a test the following error would be printed:

  1. An error occurred. /batch?id=21&op=do
  2. Fatal error: Call to undefined function install_no_profile_error() in /www/includes/install.inc on line 277

Digging about in the core code didn't really help much, the line in question doesn't seem to have a lot to do with anything, and the function install_no_profile_error() does exist. I eventually figured out that the error was being caused because when Drupal Simpletest starts up it uses the default instillation profile to create a dummy database that will be used for each test to run. In this particular instance Drupal had been set up using Aquia and for some reason the default instillation profile in the profiles directory was missing. This meant that when Drupal Simpletest was trying to use this profile to install itself it was falling over and printing out the message above.

Solving this problem is quite easy, just grab a copy of Drupal from the drupal.org site and copy over the default instillation profile into the profiles directory. When you re-run the tests they should all work.

Comments

Permalink
You've saved me a lot of time and pain. Thanks for this helpful post.

Carles (Thu, 12/30/2010 - 13:00)

Permalink

That was super easy, and saved me a huge potential headache...

Now if Drupal would just switch unit tests to PHPUnit instead...

Max (Mon, 07/25/2011 - 21:16)

Permalink

You said, "I eventually figured out that the error was being caused because when Drupal Simpletest starts up it uses the default instillation profile to create a dummy database that will be used for each test to run."

You mean for just DrupalWebTestCase; or for DrupalWebTestCase and DrupalUnitTestCase? I guess what i am asking is this -- does DrupalUnitTestCase create a dummy database, even though the tests cannot use it?

Anonymous (Wed, 12/07/2011 - 23:05)

Permalink
Great post, i enjoy reading it. Thanks bro ;)

gorka (Fri, 05/01/2015 - 20:09)

Add new comment

The content of this field is kept private and will not be shown publicly.