4

I'm building some modules onto an existing and somewhat active site. From my understanding, SimpleTest creates a new, temporary database and effectively reinstalls a copy of Drupal.

However, I'd like to take over some current items for the existing site such as taxonomy and content types and test against this data.

Do I have to manually do this all in setup()? Or is there a way to clone or import existing tables into the testing database that SimpleTest sets up?

1 Answer 1

5

The testing environment is by default completely separated from your website, so module testing is in no way affected by the environment. However you might be able to switch the database. There is this article about testing on the live database: http://www.codesidekick.com/blog/break-out-simpletest-sandbox. Basically you override some variables during setup and teardown in a class extending DrupalWebTestCase:

/**
 * @file
 * Common testing class for this Drupal site.
 */
abstract class SiteTesting extends DrupalWebTestCase {

  /**
   * Overrides default set up handler to prevent database sand-boxing.
   */
  protected function setUp() {
    // Use the test mail class instead of the default mail handler class.
    variable_set('mail_system', array('default-system' => 'TestingMailSystem'));
    $this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
    $this->public_files_directory = $this->originalFileDirectory;
    $this->private_files_directory = variable_get('file_private_path');
    $this->temp_files_directory = file_directory_temp();

    drupal_set_time_limit($this->timeLimit);
    $this->setup = TRUE;
  }

  /**
   * Overrides default tear down handler to prevent database sandbox deletion.
   */
  protected function tearDown() {
    // In case a fatal error occurred that was not in the test process read the
    // log to pick up any fatal errors.
    simpletest_log_read($this->testId, $this->databasePrefix, get_class($this), TRUE);

    $emailCount = count(variable_get('drupal_test_email_collector', array()));
    if ($emailCount) {
      $message = format_plural($emailCount, '1 e-mail was sent during this test.', '@count e-mails were sent during this test.');
      $this->pass($message, t('E-mail'));
    }

    // Close the CURL handler.
    $this->curlClose();
  }
}

Then in the test, make sure to extend your newly created class:

class SiteTestingHomePageTest extends SiteTesting {
  ...
}

I have never tried this, and please be aware that depending on the test, they might alter the live database (if you add a node in the test, it will show up on the site), but it might be what you are looking for. Make database backups and remove all created entities in the end of the test.

If you do not want to affect live data, you will have to add the content types and taxonomy terms manually.

1
  • I will give this a go. I'm testing on a development server with a copy of the production database. We always have a dev - stage - prod type architecture. I'm also curious if adding a quick backup and restore could be included in the setup and tear down. Interesting suggestion. Automatic up-vote.
    – Rick
    Commented Feb 24, 2016 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.