I'm coming across what seems to be contradictory information, and I'm hoping someone here can clarify. The basic question is "what is the correct folder structure for PSR-4 style auto-discovery of classes in Drupal 8, particularly test classes, and how much flexibility is there in that structure?" (I apologize in advance about this, but I'm going to have to partially break up my question into comments, since I don't have enough reputation yet to post all the necessary links.)

I have run across the following conventions I'm about to list in subsequent comments. I'm hoping someone can clarify what the proper layout is. Or will all work? I could spin up a D8 instance and test this, but I'm afraid such a cursory examination would conceal pitfalls of one approach versus the other, in terms of community support.

  • - This page (2560451) on the basic structure of Drupal 8 modules (general, not testing) defines any class under 'src/' as being discoverable. This is in keeping with the PSR-4 [spec] as established by xautoload – Aron Feb 25 '16 at 22:02
  • - The page on converting (2166895) D7 modules to D8 describes discoverability of module test classes when they are nested under [module_root]/src/tests. – Aron Feb 25 '16 at 22:02
  • - The community tutorial "A practical guide to building Drupal 8 modules" (2560405) describes (2560531) simpletest tests being placed under [module_root]/src/Tests. – Aron Feb 25 '16 at 22:03
  • - "PHPUnit file structure, namespace, and required metadata" places the files under [module_root]/tests/src, and references core/phpunit.xml.dist which shows a degree of authority. – Aron Feb 25 '16 at 22:03
  • - The drupal.org/node/2156625) on PSR-4 autoloading in Drupal places the tests folder under [module_root]/tests/src – Aron Feb 25 '16 at 22:03

There are three different testing frameworks in Drupal 8.

1. Simpletest: Functional, Upgrade and Unit Tests

These should be located underneath src\Tests per the documentation linked in the Question comments. This corresponds to the namespace Drupal\my_module\Tests.

This is documented best at https://www.drupal.org/node/2166895.

2. PHPUnit: Unit Tests

Theoretically these could be anywhere with a phpunit.xml.dist file, but if you need to support running through Drupal's test runner (run-tests.sh) or drupal.org infrastructure then these must be placed in tests\src\Unit and the namespace corresponds to Drupal\Tests\my_module\Unit

3. Behat: Behavioral Tests

I think there is a movement to store behavioral tests in a directory parallel to tests\src\unit, but no official convention as Drupal core does not utilize Behat yet.

  • Thanks - I can't upvote the original answerer (sorry @kiamlaluno), but I appreciate the extra linking here. Could you expand on the bit about phpunit.xml.dist? I know there's the core variant, which I assume one should avoid modifying directly, and I also assume is the sole means by which the core run-tests.sh script determines test file locations? If I wanted to have both functional and unit tests in my D8 module, and I wanted to avoid having both src/tests and tests/src, is there a way to do so? – Aron Feb 25 '16 at 23:22
  • I think this answer could use a bit of work. Behat really is irrelevant at this point as core doesn't support it and there are no rules about that. And it's also possible to write kernel and web tests with PHPUnit, see BrowserTestBase, which uses mink instead of simpletest. See drupal.org/node/2489956 and drupal.org/node/2469723 for the change records. With those, you can have all your tests in tests/src. – Berdir Feb 26 '16 at 0:18

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.