How to Test Drupal Code

Enable the Simpletest module., Look through the tests already available on your site.,Try out the tests that are already available., Get your module ready for writing tests., Add your first test case class., Write some testing code., Write an...

9 Steps 1 min read Medium

Step-by-Step Guide

  1. Step 1: Enable the Simpletest module.

    Make sure you're logged into your Drupal website as an administrator.

    Using the administrative menu, go to the 'Modules' page, find 'Simpletest'

    and click to enable it.

    It should already be installed with the Drupal core. , In any Drupal website, all core modules and many contrib modules that are installed will already have many tests written for Simpletest.

    You can see all of them by going to 'Configuration' > 'Development' > 'Testing'.

    Looking through them should give you a better idea of what you can test and how you can label your tests.,, In your module folder, add a file named 'modulename.test'.

    This is a PHP file that you should add all your tests to.

    Then in your .info file, tell Drupal about the file using files[] = modulename.test., <?php /** * @file * Description of file. */ class ModuleNameUnitTestsTestCase extends DrupalUnitTestCase { public static function getInfo() { // Note: getInfo() strings should not be translated. return array( 'name' => 'Functions tests'

    'description' => 'Test that various functions work properly.'

    'group' => 'Module Name'

    ); } } , Imagine that you've written this function, and you'd like to test that it works properly: function negate($int) { return $int *
    -1; } You could write a test (in your modulename.test file, after the getInfo() method) like this: public function testNegates() { $result = negate(5); } , At the moment, the method in the previous step doesn't actually check the result of the test in any way.

    To make sure that the value is what we expect, we write an assertion.

    In this case, we'll add assertEqual() to our method to check that the result equals
    -5.

    Also, we'll add a message and group to our assertion, which will be helpful when we're running the test later. public function testNegates() { $message = "negate() should return a negative integer."; $group = "Calculation"; $result = negate(5); $this->assertEqual($result,
    -5, $message, $group); } , In Drupal, go back to the Testing page.

    Among the list of tests, you should see the name of your module (if 'group' in your getInfo() method equals your module name).

    If you don't see it, you might need to flush the Drupal cache (specifically, the 'class' cache).

    Now enable your test using the checkbox, and select 'Run tests'.

    After a few seconds, you should see your test, and the result! It should be red if the test failed, and green if it succeeded., You can learn more about the available assertions on the DrupalTestCase page in the Drupal API.
  2. Step 2: Look through the tests already available on your site.

  3. Step 3: Try out the tests that are already available.

  4. Step 4: Get your module ready for writing tests.

  5. Step 5: Add your first test case class.

  6. Step 6: Write some testing code.

  7. Step 7: Write an assertion that checks the output of the testing code.

  8. Step 8: Run your first test.

  9. Step 9: Learn more about Simpletest.

Detailed Guide

Make sure you're logged into your Drupal website as an administrator.

Using the administrative menu, go to the 'Modules' page, find 'Simpletest'

and click to enable it.

It should already be installed with the Drupal core. , In any Drupal website, all core modules and many contrib modules that are installed will already have many tests written for Simpletest.

You can see all of them by going to 'Configuration' > 'Development' > 'Testing'.

Looking through them should give you a better idea of what you can test and how you can label your tests.,, In your module folder, add a file named 'modulename.test'.

This is a PHP file that you should add all your tests to.

Then in your .info file, tell Drupal about the file using files[] = modulename.test., <?php /** * @file * Description of file. */ class ModuleNameUnitTestsTestCase extends DrupalUnitTestCase { public static function getInfo() { // Note: getInfo() strings should not be translated. return array( 'name' => 'Functions tests'

'description' => 'Test that various functions work properly.'

'group' => 'Module Name'

); } } , Imagine that you've written this function, and you'd like to test that it works properly: function negate($int) { return $int *
-1; } You could write a test (in your modulename.test file, after the getInfo() method) like this: public function testNegates() { $result = negate(5); } , At the moment, the method in the previous step doesn't actually check the result of the test in any way.

To make sure that the value is what we expect, we write an assertion.

In this case, we'll add assertEqual() to our method to check that the result equals
-5.

Also, we'll add a message and group to our assertion, which will be helpful when we're running the test later. public function testNegates() { $message = "negate() should return a negative integer."; $group = "Calculation"; $result = negate(5); $this->assertEqual($result,
-5, $message, $group); } , In Drupal, go back to the Testing page.

Among the list of tests, you should see the name of your module (if 'group' in your getInfo() method equals your module name).

If you don't see it, you might need to flush the Drupal cache (specifically, the 'class' cache).

Now enable your test using the checkbox, and select 'Run tests'.

After a few seconds, you should see your test, and the result! It should be red if the test failed, and green if it succeeded., You can learn more about the available assertions on the DrupalTestCase page in the Drupal API.

About the Author

D

Debra Clark

Creates helpful guides on lifestyle to inspire and educate readers.

40 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: