7

I'm trying out the built in test driven development method in Drupal 8 but I'm running in to a problem..

I'm getting the following errors when trying to run ./core/vendor/bin/phpunit --list-groups ./modules/ in my cli:

PHP Fatal error: Class 'Drupal\Tests\UnitTestCase' not found in /Applications/MAMP/htdocs/drupal/modules/custom_cookie_compliance/ tests/CustomCookieComplianceTest.php on line 10

Fatal error: Class 'Drupal\Tests\UnitTestCase' not found in /Applications/MAMP/htdocs/drupal/modules/custom_cookie_compliance/tests/ CustomCookieComplianceTest.php on line 10

I have created a module folder called custom_cookie_compliance in my modules folder. In turn I have a custom_cookie_compliance.info.yml file containing the following.

 name: Custom Cookie Compliance
 description: Displays a cookie compliance information window at the top of the website.
 core: 8.x
 type: module

I also have a tests folder containing a src folder which then contains a CustomCookieComplianceTest.php file with the following code.

<?php
namespace Drupal\custom_cookie_compliance\Tests;

use Drupal\Tests\UnitTestCase;

/**
 * @group cookie
 */

class CustomCookieComplianceTest extends UnitTestCase {
  public function testSetCookies() {
    if (!isset($cookie_set)) {
      $cookie_set = TRUE;
    }

    $this->assertTrue($cookie_set);
  }
}

I have written the code by following this guide.

I understand it can't find the UnitTestCase class, but the guide says that is should work, so why isn't it?

10

My guess is that you're not including the phpunit.xml.dist file, which sits in the core folder.

Either run tests from there (./vendor/bin/phpunit ../modules/your_module) or specify that file with -c.

You can also provide your own phpunit.xml.dist file (see the Payment module as an example); then you can run PHPUnit from your module folder.

  • I haven't run it from the core folder as you say so I will have to check it out on monday, ill get back to you then! It sounds like it could be whats causing the problem. – jzasnake Mar 14 '15 at 22:06
  • Hey thanks for the help, it certainly solved the problem! – jzasnake Mar 18 '15 at 7:29
  • 2
    Thanks a bunch. This fixed my issue. For those looking at this, I ran this command from CLI: vendor/bin/phpunit -c web/core/phpunit.xml.dist web/modules/custom – Jorge Calderon Oct 6 '17 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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