1

I am trying to run a BrowserTestBase based test in my own module, following the documentation on drupal.org.

No matter if I run the test from PHPStorm or command line, I get the same error:

vagrant@local:/var/www/mysite/docroot$ ../vendor/bin/phpunit -c core/phpunit.xml --group mymodule
PHP Fatal error:  Trait 'Drupal\Tests\facets\Functional\BlockTestTrait' not found in /var/www/mysite/docroot/modules/contrib/search_api_solr/tests/src/Functional/FacetsTest.php on line 20
PHP Stack trace:
PHP   1. {main}() /var/www/mysite/vendor/phpunit/phpunit/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /var/www/mysite/vendor/phpunit/phpunit/phpunit:52
PHP   3. PHPUnit_TextUI_Command->run() /var/www/mysite/vendor/phpunit/phpunit/src/TextUI/Command.php:100
PHP   4. PHPUnit_TextUI_Command->handleArguments() /var/www/mysite/vendor/phpunit/phpunit/src/TextUI/Command.php:111
PHP   5. PHPUnit_Util_Configuration->getTestSuiteConfiguration() /var/www/mysite/vendor/phpunit/phpunit/src/TextUI/Command.php:649
PHP   6. PHPUnit_Util_Configuration->getTestSuite() /var/www/mysite/vendor/phpunit/phpunit/src/Util/Configuration.php:878
PHP   7. PHPUnit_Framework_TestSuite->addTestFile() /var/www/mysite/vendor/phpunit/phpunit/src/Util/Configuration.php:1000
PHP   8. ReflectionMethod->invoke() /var/www/mysite/vendor/phpunit/phpunit/src/Framework/TestSuite.php:353
PHP   9. Drupal\Tests\TestSuites\FunctionalTestSuite::suite() /var/www/mysite/vendor/phpunit/phpunit/src/Framework/TestSuite.php:353
PHP  10. Drupal\Tests\TestSuites\TestSuiteBase->addTestsBySuiteNamespace() /var/www/mysite/docroot/core/tests/TestSuites/FunctionalTestSuite.php:22
PHP  11. PHPUnit_Framework_TestSuite->addTestFiles() /var/www/mysite/docroot/core/tests/TestSuites/TestSuiteBase.php:55
PHP  12. PHPUnit_Framework_TestSuite->addTestFile() /var/www/mysite/vendor/phpunit/phpunit/src/Framework/TestSuite.php:384
PHP  13. PHPUnit_Util_Fileloader::checkAndLoad() /var/www/mysite/vendor/phpunit/phpunit/src/Framework/TestSuite.php:310
PHP  14. PHPUnit_Util_Fileloader::load() /var/www/mysite/vendor/phpunit/phpunit/src/Util/Fileloader.php:38
PHP  15. include_once() /var/www/mysite/vendor/phpunit/phpunit/src/Util/Fileloader.php:56

Fatal error: Trait 'Drupal\Tests\facets\Functional\BlockTestTrait' not found in /var/www/mysite/docroot/modules/contrib/search_api_solr/tests/src/Functional/FacetsTest.php on line 20

I am not requiring Search API Solr in my test, nor do I have a clue why it is running it. Even if I specify --testsuite unit, I get the same error.

Here is the test I am trying to run, with a basic test step:

namespace Drupal\Tests\mymodule\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * Class AuthenticationTest.
 *
 * @group mymodule
 */
class AuthenticationTest extends BrowserTestBase {

  /**
   * Required modules.
   *
   * @var array
   */
  protected static $modules = ['user', 'node'];

  public function setUp() {
    parent::setUp();

    // set config for netforum
  }

  /**
   * The superuser should not authenticate to Netforum.
   */
  public function testAdminUserUsesDrupalAuthentication() {
    $account = $this->drupalCreateUser(['administer site configuration']);
    $this->drupalLogin($account);
  }

}

I am not sure why it is even looking at the Search API Solr module, but the test in question does try to use several files that do not exist:

use Drupal\Tests\facets\Functional\BlockTestTrait;
use Drupal\Tests\facets\Functional\ExampleContentTrait;
use Drupal\Tests\facets\Functional\TestHelperTrait;

I assume these are a part of the Facets module, which I do not have.

edit: This might be my issue.

1 Answer 1

2

edit: This might be my issue.

Yes.

The error happens during test discovery, first it has to find all tests and then limit it based on the group.

The easiest workaround is simply to specifiy your module, then you don't even need the group. phpunit will only discover test in that folder, which will not just avoid this problem it will also be much faster.

$ ../vendor/bin/phpunit -c core/phpunit.xml modules/custom/mymodule
2
  • Thanks, lifesaver. The patch in the issue there does not work, and I cannot just dl 8.4.x for @requires, and the required modules are listed in require-dev there... messy.
    – Kevin
    Commented Jul 27, 2017 at 15:32
  • One note - PHPStorm complains about the directory path since I am running a VM, even though I am using remote interpreter and have path mappings correct. Running from the CLI works in this one instance, though that module should fix this because it blocks a lot of folks from testing or attempting to learn to do functional tests.
    – Kevin
    Commented Jul 27, 2017 at 15:45

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.