Two Beginner Errors In Drupal 6 Simpletest

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 weird 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.

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

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.

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.

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:

An error occurred. /batch?id=21&op=do
Fatal error: Call to undefined function install_no_profile_error() in /www/includes/install.inc</b> 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 Acquia 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

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

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

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

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?

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

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
4 + 10 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.