1

I'm working on an 8.x module that uses PHPUnit tests. I've been running these from the command line using the embedded phpunit script that's installed with Drupal 8:

../vendor/bin/phpunit PATH/TO/MY/FILE.php

Both of my tests work w/o any problems under both PHP 5.5 and PHP 5.6.

But the Drupal testbot is failing both of my tests with the error

Fatal error: Trait 'Drupal\Tests\my_module\MockSessionTrait' not found in /var/www/.../PATH/TO/MY/FILETest.php

Using phpunit from the command line, this same trait is found w/o any trouble.

If I run the tests myself from the command line, but this time using run-tests.sh, I get the same error the testbot gets:

php scripts/run-tests.sh --url http://192.168.88.42/drupal --class 'Drupal\Tests\my_module\Unit\FILETest'

The stack trace looks like this:

PHP Stack trace:
PHP   1. {main}() /var/www/drupal/core/scripts/run-tests.sh:0
PHP   2. simpletest_script_get_test_list() /var/www/drupal/core/scripts/run-tests.sh:76
PHP   3. class_exists() /var/www/drupal/core/scripts/run-tests.sh:807
PHP   4. spl_autoload_call() /var/www/drupal/core/scripts/run-tests.sh:807
PHP   5. Symfony\Component\ClassLoader\ApcClassLoader->loadClass() /var/www/drupal/core/scripts/run-tests.sh:0
PHP   6. require() /var/www/drupal/vendor/symfony/class-loader/ApcClassLoader.php:114

I'm guessing that I need to help run-tests.sh to find my files, somehow.

Any advice from anyone as to why run-tests.sh behaves differently here from stock phpunit?

3
  • Please add the exact location of your test class and trait. I'm not sure but you might not be able to have a non-test class in the tests/src location, try moving that to module/src/... and change the namespace accordingly (Drupal\module\...
    – Berdir
    Oct 27, 2015 at 10:41
  • @Berdir: one class is at drupal/modules/custom/examples/file_example/tests/src/Kernel/StreamWrapperTest.php, and the trait at drupal/modules/custom/examples/file_example/tests/src/MockSessionTrait.php. You can see the patch at drupal.org/node/2102651#comment-10495064. Oct 27, 2015 at 11:20
  • @Berdir: I BOW BEFORE YOUR SUPERIOR INTELLECT :-) I moved the trait to Drupal\file_example\StreamWrapper\MockSessionTrait, and damned if everything now works both under run-tests.sh and ../vendor/bin/phpunit. Yes, run-tests.sh will spit up if a PHPUnit test class references a trait that is also a Drupal\Tests class. Who knew? Well, you did, anyway. Oct 27, 2015 at 11:38

1 Answer 1

2

@Berdir is completely correct here:

I'm not sure but you might not be able to have a non-test class in the tests/src location, try moving that to module/src/... and change the namespace accordingly (Drupal\module...

While PHPUnit doesn't mind if a test class uses a trait that's in a Drupal\Tests namespace, the simpletest test runner run-tests.sh will be unable to find the trait if you do this.

So don't do that.

Even if a trait is used only with your test classes, if you want your code to get past the Drupal testbot, the trait needs to live somewhere in your Drupal\your_module\ namespace. Do this, and both phpunit and run-tests.sh will be happy.

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.