11

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?

2 Answers 2

12

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.

1
  • 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 Commented Oct 6, 2017 at 2:11
0

I ran into the same issue today and noticed that I was using an older version of PHP Unit. After upgrading to the latest, the tests worked.

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.