Recently, I have made two major improvements to debugging in Drupal 7, the addition of a general debug function and a verbose mode for SimpleTest. The two additions make it much easier to debug problems quickly through the use of a consistent method. Take a look at what chx said via twitter:
Writing #drupal code? Check the new function debug(). Writing #drupal tests? Check $this->verbose(). And debug() works too. AWESOME!
General debug function
The general debug function can be used at any point after Drupal is bootstrapped, although the limitation may be removed in the future. The function provides a very simple wrapper to dump data through the use of
print_r(). When used normally it will display data based on the "Logging and errors" settings provided in Drupal 7 core. If using a dev version the debug information will be displayed using
drupal_set_message() as shown in the screenshot below.
The exciting part about the new
debug() function is that it also works during testing. The
debug() function can be placed inside the test itself or in any other part of Drupal and it will be picked up and displayed in the test results as shown below.
SimpleTest verbose mode
Another exciting new debugging tool that is extremely useful when writing tests is the new verbose mode for Drupal 7 SimpleTest. The verbose mode can be enabled on the SimpleTest settings page.
Once enabled SimpleTest will automatically record the page as it was seen by the SimpleTest browser after each
drupalPost() call. A link is then placed in the test results that will display the page the browser saw and some meta data related to the request.
In addition to the automatic message provided by SimpleTest custom verbose data may be dumped using
DrupalWebTestCase->verbose() which can be used in a test as shown.
If the data to be dumped in not available in the test, but in the code being tested a function is provided that may be accessed by including the
DrupalWebTestCase as shown below.
require_once drupal_get_path('module', 'simpletest') . '/drupal_web_test_case.php';
By adding these debugging tools to Drupal 7 the developer experience involved in writing a test has been greatly improved. These methods can still be improved and as such please feel free to file issues in the Drupal 7 SimpleTest issue queue. Also note that this work was sponsored by Acquia as part of my Summer Internship.