automated testing in drupal
DESCRIPTION
Automated testing in Drupal Presentation for first Drupal Cafe in Zaprojie, Ukraine 21 July 2011TRANSCRIPT
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