Testing contrib module integration with other contrib modules

Events happening in the community are now at Drupal community events on www.drupal.org.
dave reid's picture

I am writing a test for one of my contrib modules that has a feature that is enabled when another contrib module (we'll call it OtherModule) is enabled. I want other people other than myself to be able to run the entire test suite, but what if they didn't have OtherModule installed or even present in their Drupal installation? The test is going to fail spectacularly in that case. So here's what I came up with for my module:

<?php
if (($file = db_result(db_query("SELECT filename FROM {system} WHERE type = 'module' AND name = 'othermodule'"))) && file_exists($file)) {
  class
MyModuleOtherModuleIntegrationTestCase extends DrupalWebTestCase {
    function
getInfo() {
      return array(
       
'name' => t('OtherModule integration'),
       
'description' => t('Test MyModule integration with OtherModule.'),
       
'group' => t('MyModule'),
    }

    function
setUp() {
     
parent::setUp('mymodule', 'othermodule');
    }

    function
testIntegration() {
     
$this->assertTrue(module_exists('othermodule'), 'OtherModule enabled');
     
// Perform more testing...
   
}
  }
}
?>

I re-used the idea from module_load_include and drupal_get_filename to check if the current module is present in the {system} table and that the file also exists. Keep in mind the check for file_exists is necessary since Drupal doesn't remove records from {system} of deleted files/modules.

Going through this got me thinking...what if I could just specify that this test requires a module using getInfo()? This will admittedly not be very useful for any of the core tests, since we are assured they will always exist. I think this might be useful for contrib module tests that need to test integration with another (optional) contrib module. The implementation in getInfo() would look like the following:

<?php
class MyModuleOtherModuleIntegrationTestCase extends DrupalWebTestCase {
  function
getInfo() {
    return array(
     
'name' => t('OtherModule integration'),
     
'description' => t('Test MyModule integration with OtherModule.'),
     
'group' => t('MyModule'),
     
'requires' => array('othermodule'),
  }
  ...
}
?>

Any ideas on if this should be something we should implement in SimpleTest (and the core HEAD module)?

Comments

HEAD SimpleTest issue posted

dave reid's picture

I just posted the issue to get this into HEAD at http://drupal.org/node/343502

Senior Drupal Developer for Lullabot | www.davereid.net | @davereid

This reminded me of an issue

jhedstrom's picture

This reminded me of an issue I came across while writing tests for a module that was a 3rd-party integration module. There was no way to have the tests against the 3rd party API work without, in this example, a username, password and developer key. Once these 3 things were in place, the tests could proceed. The hacky solution I came up with was to append a small settings array to the settings.php file.

Ideally, similar to the above syntax, there would be a way to indicate that certain parameters need to be filled out prior to tests being available.This is another instance of the need not being at all present in core, but fairly important for contrib. My initial thought was that the test in question could define a config path, and the SimpleTest framework would pick up on that and force the user to that path prior to enabling the tests for that module.

Testing and Quality Assurance

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: