Did this article
help you?

This helped me

How to Test Drupal Code

This article will guide you in starting to write tests for your Drupal code. We'll assume that the code you're writing is primarily within a custom or 'contrib' (community-contributed) Drupal module.There are many ways of testing a Drupal website, but the most popular ways to test Drupal code is unit testing and integration testing. This article will cover unit testing.The easiest way to write and execute tests in Drupal 7 and Drupal 8 is using the Simpletest module, which this guide will cover. However, note that the Drupal core developers have started to use a different testing framework - PHP Unit - for Drupal 8 development.

Steps

  1. 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.
  2. 2
    Look through the tests already available on your site. 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.
    Advertisement
  3. 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' 
       , 
       ); 
       } 
       } 
      
  4. 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 
       ); 
       } 
      
  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 
       ); 
       } 
      
  6. 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.
  7. You can learn more about the available assertions on the DrupalTestCase page in the Drupal API.
    Advertisement

Expert Q&A

Ask a Question

      Advertisement

      Tips

      • Test early. As soon as you write code that adds new functionality or fixes broken functionality, write a test to make sure it works as expected.
      • Test often. Keep writing tests as you write code so that it becomes a part of your process, and doesn't become a chore you leave to the end and ignore.
      • Test automatically. If possible, use tools that allow your tests to be run regularly without you having to run the tests yourself.
      Show More Tips
      Advertisement

      About this article

      Thanks to all authors for creating a page that has been read 21,874 times.

      Is this article up to date?

      Advertisement