AUTOMATED TESTING IN DRUPAL Artem Berdishev
What is SimpleTest? • PHP Test Framework • A way to create and run automated regression tests • A functional testing environment: A pseudo-user navigates in a pseudo-browser and checks the results. • Manipulation (create user, drupalGet(), drupalPost()) • Assertions (assertText(), assertPattern(), etc.) • A unit test class is also available, but the general emphasis is on functional testing.
Why Automated Testing? • Define exactly what the code should do • Easy development • Easy to refactor code • Less debugging • Less mistakes • Document Bugs • Prevent regressions • Quality Assurance • Better Software
Why Automated Testing? • Once a test is written, it will likely be run many many times • Every time it is run, it verifies that the code is still working • If it catches even one bug, it has “paid for itself” – high value • Can be used to help reproduce problems
How To Install? • In Drupal 7, Simpletest is preinstalled. • In Drupal 6, it must be installed as a contrib module and a patch must be applied to core - see the INSTALL.txt. • Enable simpletest module on the modules page (or "drush en simpletest") • Configure "verbose" mode at admin/build/testing/settings (on by default in D7) • Check the status page for problems (admin/reports/status)
so… how does it works? • .test files • Contain test classes • Test classes extend the SimpleTest class • DrupalUnitTestCase • DrupalWebTestCase • Use Helper methods to: • Simulate user actions • Check behavior
Create the class and getInfo()
Create a setUp() for the test class
Create Test Methods
Test Results
API functions • drupalGet($path, $options = array()) • drupalPost($path, $edit, $submit, array $options = array(), array $headers = array()) • drupalCreateUser($permissions = NULL) • drupalLogin($user = NULL) • drupalLogout() • randomString($number = 8) • randomName($number = 8)
Assertions • assertTrue($result, $message = '', $group = 'Other') • assertNull($value, $message = '', $group = 'Other') • assertEqual($first, $second, $message = '', $group = 'Other') • assertIdentical($first, $second, $message = '', $group= 'Other') • assertPattern($pattern, $message = '', $group = 'Other') • assertText($text, $message = '', $group = 'Other') • assertTitle($title, $message = '', $group= 'Other') • assertLink($label, $index = 0, $message = '', $group = 'Other') • assertResponse($code, $message = '') • pass($message = NULL, $group = 'Other') • fail($message = NULL, $group = 'Other') • verbose($message)
Resources • http://drupal.org/simpletest • http://groups.drupal.org/unit-testing • The SimpleTest tutorial: drupal.org/simpletest- tutorial-drupal7 (D7) or drupal.org/simpletest- tutorial (D6) • http://drupal.org/project/examples
AUTOMATED TESTING IN DRUPAL Artem Berdishev email: berdartem@gmail.com skype: berdishev

Automated testing in Drupal

  • 1.
  • 2.
    What is SimpleTest? •PHP Test Framework • A way to create and run automated regression tests • A functional testing environment: A pseudo-user navigates in a pseudo-browser and checks the results. • Manipulation (create user, drupalGet(), drupalPost()) • Assertions (assertText(), assertPattern(), etc.) • A unit test class is also available, but the general emphasis is on functional testing.
  • 3.
    Why Automated Testing? •Define exactly what the code should do • Easy development • Easy to refactor code • Less debugging • Less mistakes • Document Bugs • Prevent regressions • Quality Assurance • Better Software
  • 4.
    Why Automated Testing? •Once a test is written, it will likely be run many many times • Every time it is run, it verifies that the code is still working • If it catches even one bug, it has “paid for itself” – high value • Can be used to help reproduce problems
  • 5.
    How To Install? • In Drupal 7, Simpletest is preinstalled. • In Drupal 6, it must be installed as a contrib module and a patch must be applied to core - see the INSTALL.txt. • Enable simpletest module on the modules page (or "drush en simpletest") • Configure "verbose" mode at admin/build/testing/settings (on by default in D7) • Check the status page for problems (admin/reports/status)
  • 6.
    so… how doesit works? • .test files • Contain test classes • Test classes extend the SimpleTest class • DrupalUnitTestCase • DrupalWebTestCase • Use Helper methods to: • Simulate user actions • Check behavior
  • 8.
    Create the classand getInfo()
  • 9.
    Create a setUp()for the test class
  • 10.
  • 11.
  • 12.
    API functions • drupalGet($path, $options = array()) • drupalPost($path, $edit, $submit, array $options = array(), array $headers = array()) • drupalCreateUser($permissions = NULL) • drupalLogin($user = NULL) • drupalLogout() • randomString($number = 8) • randomName($number = 8)
  • 13.
    Assertions • assertTrue($result, $message= '', $group = 'Other') • assertNull($value, $message = '', $group = 'Other') • assertEqual($first, $second, $message = '', $group = 'Other') • assertIdentical($first, $second, $message = '', $group= 'Other') • assertPattern($pattern, $message = '', $group = 'Other') • assertText($text, $message = '', $group = 'Other') • assertTitle($title, $message = '', $group= 'Other') • assertLink($label, $index = 0, $message = '', $group = 'Other') • assertResponse($code, $message = '') • pass($message = NULL, $group = 'Other') • fail($message = NULL, $group = 'Other') • verbose($message)
  • 14.
    Resources • http://drupal.org/simpletest • http://groups.drupal.org/unit-testing • The SimpleTest tutorial: drupal.org/simpletest- tutorial-drupal7 (D7) or drupal.org/simpletest- tutorial (D6) • http://drupal.org/project/examples
  • 15.
    AUTOMATED TESTING IN DRUPAL Artem Berdishev email: berdartem@gmail.com skype: berdishev