Drupal 6 PHPUnit testing setup

Using PHPUnit or PHPSpec when developing Drupal modules has some obvious benefits; you don’t have to reload you entire website in a browser each time you want to test your module.I have spent the last couple of months developing modules for Drupal 5 and 6 and I tend to spent too much time clearing the cache and the theme registry when using the hook_menu (). The problem is that you need to tell Drupal about the new menu item before you can call it, even in a test case.

If you want to use PHPUnit when developing Drupal modules, and if you want to have a folder structure where tests are not stored together with you actual modules – tests do not belong in any production environment – this guide will be helpful.

To make unit testing agnostic in Drupal you need to store your tests at the root level of your Drupal directory, I prefer this structure for my Drupal projects:

Root:
- includes
- misc
- modules
- profiles
- scripts
- sites
- tests
- themes

Notice the “tests” folder in the root, that’s where all our PHPUnit tests will go.

To get this running you will need to include Drupal’s bootstrap inside your PHPUnit test case, and you can use the following set_include_path script:

define('ROOT_PATH', realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR));
set_include_path(ROOT_PATH
. PATH_SEPARATOR
. get_include_path()
);

Now you can safely include the bootstrap.inc file located inside the includes folder in your root directory like so:

include_once 'includes' . DIRECTORY_SEPARATOR . 'bootstrap.inc';

and once that’s done, you can bootstrap the testcase:

drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

That’s it, now your ready to use PHPUnit when developing Drupal modules, and to do so, cd into your root directory and execute the following PHPUnit code:

phpunit ModuleTest tests/ModuleTest.php

That’s it, enjoy.

About these ads

2 responses to “Drupal 6 PHPUnit testing setup

  1. Madhavi

    In linux server we are installed phpunit and drupal7
    If we add “drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL)” this bootstrap not running phpunit
    Please help me

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: