http://drupal.org/node/394888

DrupalUnitTestCase was reintroduced by #464714: Speed up the tests. From the doxygen:
/**
* 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(), drupal_function_exists(), module_implements(),
* module_invoke_all() etc.
*/

However, it is much quicker than DWTC for what it can do. "chx: If i clock my CPU enough to time this i get 4s vs 0s..."

The page needs:
- at the end of the 1st sentence ", although unit testing is supported."
- at the end of the section "Separate testing environment" the following new paragraph—
"The database tables and files directory are not created for unit tests. This makes them much faster to initialise than functional tests but means that they cannot access the database or the files directory. Calling any Drupal function that needs the database will throw exceptions."

The comment, changing the testing url, needs to be rolled in and deleted too.

Marking critical b/c testing is so important to the dev process, especially nowish. We want people to be implementing this stuff when upgrading their modules. I have added a section on unit testing to the Simpletest tutorial, which I could edit: http://drupal.org/node/395012

Comments

arianek’s picture

Issue tags: +Simpletest, +d7docs, +handbook, +testing

adding tags

Rok Žlender’s picture

Assigned: Unassigned » Rok Žlender
Status: Active » Needs review

As I don't have access to edit the http://drupal.org/simpletest-tutorial-drupal7 directly pasting unit test instructions here.

Writing unit tests requires a bit different approach than functional testing. Steps involved in writing an unit test:
<ul>
 <li>Creating the structure (class needs to inherit from DrupalUnitTestCase)</li>
 <li>Any required files need to be included manually in setUp function, enabling modules will not work as we are not accessing database in unit tests</li>
 <li>Create actual tests with the test case</li>
</ul>

To start, we need to create a class that extends DrupalUnitTestCase.

<?php
class UnittestExampleTestCase extends DrupalUnitTestCase {
}
?>

To make our test case available in the interface, we need to implement getInfo() same as with DrupalWebTestCase.

<?php
  public static function getInfo() {
    return array(
      'name' => 'Unittest Example',
      'description' => 'Ensure that the example_function works correctly.',
      'group' => 'Examples',
    );
  }
?>

As unit testing does not use database we need to manually include any required files.

<?php
  function setUp() {
    require_once DRUPAL_ROOT . '/modules/example/example.inc';
    parent::setUp();
  }
?> 

Now we need to create a test that will exercise the unit of code we are testing. For unit testing to be possible code that we are testing needs to always return same value for same parameters passed in. If code other than the code we are testing affects the return we cannot reliably test it.

<?php
  public function testExampleFunction() {
    $expected_result = 'example return';
    $input = 'example input',
    $result = example_function($input);
    $this->assertEqual($result, $expected_result, $expected_result, t('Example function returned expected result.));
  }
?>
Rok Žlender’s picture

Revised. Also created an issue to add code to example module #1261014: Add unit test example to simpletest example module.

Writing unit tests requires a bit different approach than functional testing. Steps involved in writing an unit test:
<ul>
  <li>Creating the structure (class needs to inherit from DrupalUnitTestCase)</li>
  <li>Any required files need to be included manually in setUp function, enabling modules will not work as we are not accessing database in unit tests</li>
  <li>Create actual tests with the test case</li>
</ul>

To start, we need to create a class that extends DrupalUnitTestCase.

<?php
class UnittestExampleTestCase extends DrupalUnitTestCase {

}
?>

To make our test case available in the interface, we need to implement getInfo() same as with DrupalWebTestCase.

<?php
  public static function getInfo() {
    return array(
      'name' => 'Simpletest unit test Example',
      'description' => 'Ensure that the simpletest_example_function works correctly.',
      'group' => 'Examples',
    );
  }
?>

As unit testing does not use database we need to manually include any required files.

<?php
  function setUp() {
    drupal_load('module', 'simpletest_example');
    parent::setUp();
  }
?> 

Now we need to create a test that will exercise the unit of code we are testing. For unit testing to be possible code that we are testing needs to always return same value for same parameters passed in. If code other than the code we are testing affects the return we cannot reliably test it.

<?php
  public function testSimpletestUnitTestExampleFunction() {
    $expected_result = 'example return';
    $input = 'example input';
    $result = simpletest_example_function($input);
    $this->assertEqual($result, $expected_result, t('Example function returned expected result.'));
  }
?>

Now you only need to come up with the most impossible input for your code and hope that it returns what you expect it to. Assertion functions available for unit tests are listed in http://api.drupal.org/api/drupal/modules--simpletest--drupal_web_test_case.php/class/DrupalUnitTestCase/7 and there are good examples of unit tests in core simpletest module.
Rok Žlender’s picture

I should read through all the docs first http://drupal.org/node/811254 holds unit test docs. IMO we should move it to the same level as SimpleTest Tutorial (Drupal 7) and merge the example into example module.

arianek’s picture

You're a star! Do you need a technical review? I can always tweet a link out and see if anyone will look at it, otherwise issues tend to stagnate...

Rok Žlender’s picture

I will review the existing unit test docs and provide example code for it in example module docs above will not be used.

willmoy’s picture

Brill! Lovely to see action on this. Am heading away for three weeks but if still needs sorting will pick this up then.

On a quick read this all looks good technically, needs some light grammar editing e.g.
"As unit testing does not use database we need to manually include any required files." < missing 'the' before database

But example.module seems like a good fit, with handbook page updated accordingly.

Thanks very much, Rok.

Rok Žlender’s picture

Status: Needs review » Fixed

Docs are updated at http://drupal.org/node/811254 example module code got committed in #1261014: Add unit test example to simpletest example module I think this one is done.

Status: Fixed » Closed (fixed)
Issue tags: -Simpletest, -d7docs, -handbook, -testing

Automatically closed -- issue fixed for 2 weeks with no activity.