Answers.Body: <p>I'm not an expert of testing but i'm pretty sure that SimpleTest creates a new instance of Drupal with a new database each time the testing process is ran.</p>
  
  <p>CiviCRM is not a standard Drupal module and cannot just be enabled. In your <code>SetUp</code> function, You need to install CiviCRM to create the corresponding tables in the database. It could be a rather complex task but it's possible to do so. There is a drush script to do it so it might be a good inspiration.</p>
  
  <p>You might want to check <code>drush_civicrm_install</code> in <a href="https://github.com/civicrm/civicrm-drupal/blob/7.x-master/drush/civicrm.drush.inc" rel="nofollow">https://github.com/civicrm/civicrm-drupal/blob/7.x-master/drush/civicrm.drush.inc</a></p>
  
  <p>Otherwise, you might find easier to use CiviCRM testing way (phpUnit) : <a href="https://wiki.civicrm.org/confluence/display/CRMDOC/Testing" rel="nofollow">https://wiki.civicrm.org/confluence/display/CRMDOC/Testing</a></p>
  
  <p>And an example : <a href="https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ActivityTest.php" rel="nofollow">https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ActivityTest.php</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-04T13:57:18.673Z
Answers.LastActivityDate: 2016-05-04T13:57:18.673Z
Answers.UserDisplayName: samuelsov
Body: <p>I have written a drupal module that uses the CiviCRM api. I want to build tests for my module. Drupal 7 includes the simpletest module in core, so I'm using that. I have created the following test:</p>
  
  <pre><code>class ExampleCiviCRMTestCase extends DrupalWebTestCase {
  
  public static function getInfo(){
      return array(
          'name' =&gt; 'Example CiviCRM Test',
          'description' =&gt; 'Check that CiviCRM API can be used in tests',
          'group' =&gt; "CiviCRM",
      );
  }
  
  public function setUp() {
      parent::setUp(array('civicrm'));
  }
  
  public function testCivicrmWorks(){
      civicrm_initialize();
      try {
          $result = civicrm_api3('Contact', 'get', array(
              'sequential' =&gt; 1,
          ));
      } catch (CiviCRM_API3_Exception $e) {
          $error = $e-&gt;getMessage();
          $this-&gt;fail($error);
      }
      $this-&gt;assertFalse(empty($result['count']));
  }
  }
  </code></pre>
  
  <p>This test fails. The error page states that the civicrm module was successfully enabled, but then this error occured:
  <code>Access to undeclared static property: Database::$ignoreTargets</code>. The error occured in database.inc on line 1473. Does anyone have any experience using simpletest with civicrm?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-04-18T14:43:23.023Z
Comments.Text: I'm interested in this, too. I've done testing with CiviCRM using Selenium (drives an actual web browser), and also using phpunit - a bit of a hack (and not unit testing) but you can bootstrap Drupal and civi and then use normal php unit stuff. I realise this is not an answer to your Q, but that's why it's in a comment :-)
Comments.UserDisplayName: artfulrobot
Tags: <drupal><api><testing>
Title: Testing a module which uses CiviCRM php api
ThreadText: # Testing a module which uses CiviCRM php api
  
  (Asked by: username_0 on 2016-04-16)
  
  I have written a drupal module that uses the CiviCRM api. I want to build tests for my module. Drupal 7 includes the simpletest module in core, so I'm using that. I have created the following test:
  
  ```
  class ExampleCiviCRMTestCase extends DrupalWebTestCase {
  
  public static function getInfo(){
      return array(
          'name' => 'Example CiviCRM Test',
          'description' => 'Check that CiviCRM API can be used in tests',
          'group' => "CiviCRM",
      );
  }
  
  public function setUp() {
      parent::setUp(array('civicrm'));
  }
  
  public function testCivicrmWorks(){
      civicrm_initialize();
      try {
          $result = civicrm_api3('Contact', 'get', array(
              'sequential' => 1,
          ));
      } catch (CiviCRM_API3_Exception $e) {
          $error = $e->getMessage();
          $this->fail($error);
      }
      $this->assertFalse(empty($result['count']));
  }
  }
  
  ```
  
  This test fails. The error page states that the civicrm module was successfully enabled, but then this error occured:
  `Access to undeclared static property: Database::$ignoreTargets`. The error occured in database.inc on line 1473. Does anyone have any experience using simpletest with civicrm?
  
  **Tags:** drupal, api, testing
  
  > **Comments:**
  > **username_1**: I'm interested in this, too. I've done testing with CiviCRM using Selenium (drives an actual web browser), and also using phpunit - a bit of a hack (and not unit testing) but you can bootstrap Drupal and civi and then use normal php unit stuff. I realise this is not an answer to your Q, but that's why it's in a comment :-)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2016-05-04. Score: 2 | Accepted answer
  
  I'm not an expert of testing but i'm pretty sure that SimpleTest creates a new instance of Drupal with a new database each time the testing process is ran.
  
  CiviCRM is not a standard Drupal module and cannot just be enabled. In your `SetUp` function, You need to install CiviCRM to create the corresponding tables in the database. It could be a rather complex task but it's possible to do so. There is a drush script to do it so it might be a good inspiration.
  
  You might want to check `drush_civicrm_install` in <https://github.com/civicrm/civicrm-drupal/blob/7.x-master/drush/civicrm.drush.inc>
  
  Otherwise, you might find easier to use CiviCRM testing way (phpUnit) : <https://wiki.civicrm.org/confluence/display/CRMDOC/Testing>
  
  And an example : <https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ActivityTest.php>
  
  
  ---
  
  