In Drupal 8 core modules tests are under two different namespaces which maps to two different directories under the module.

\Drupal\Tests\<mymodule>\<TestSuiteType> => <mymodule>/tests/src/<TestSuiteType>
\Drupal\<mymodule>\Tests => <mymodule>/src/Tests direcotry.

For example in field_ui module.

\Drupal\field_ui\Tests\FieldUIDeleteTest => field_ui/src/Tests/FieldUIDeleteTest.php
\Drupal\Tests\field_ui\Functional\EntityDisplayModeTest => field_ui/tests/src/Functional/EntityDisplayModeTest.php

Can someone please explain the reason behind this and any advantage of writing test cases under two different namespace for the same module?

1 Answer 1


They're just different kinds of tests, with different requirements/dependencies, and so they're split up.

From PHPUnit file structure, namespace, and required metadata:

File structure and namespace

There's a special relationship between the location of test files within the file structure, the namespace they use, and the class which they extend.

Drupal Unit Test Suites: Auto-detected Tests

Drupal 8 uses a number of test suites to allow for choosing between different kinds of tests to run.

Drupal 8 has the following test suites:

  • Simpletest: Legacy tests based on the Drupal version of the Simpletest framework.
  • Unit: PHPUnit-based tests with minimal dependencies.
  • Kernel: PHPUnit-based tests with a bootstrapped kernel, and a minimal number of extensions enabled.
  • Functional: PHPUnit-based tests with a booted Drupal instance.
  • FunctionalJavascript: PHPUnit-based tests which use PhantomJS to perform tests of Javascript functionality.

Drupal's test system can discover these different test suites for core, core extensions, and contrib extensions, as long as the tests are placed within the proper directories so they can be discovered as belonging to one of the test suites listed above.

Simpletest-based tests must be placed in an extension, within that extension's src/Tests directory. They will have a namespace of \Drupal\$extension\Tests.

PHPUnit-based: Generally, the other suites must be within a tests/src/$suite_type/ directory. These tests will have a namespace of \Drupal\Tests\$extension\$suite_type.

Traits: The third class is traits. It is possible to write a trait which will be discovered and can be used by the various different test types. Each test suite will discover the trait if it is placed in the tests/src/Traits directory, and the trait will have a namespace of \Drupal\Tests\$extension\Traits.

Note that the class names must END with the word Test. The test methods within the class must START with the word test.

Simpletest Class, File, and Namespace structure also provides a bit more insight.

  • I would take a guess that the reason for the specific namespace conventions Drupal uses are just to fall in line with whatever's easiest out-of-the-box for the test libraries being used
    – Clive
    Commented Mar 8, 2018 at 12:32
  • Thanks @Clive. How can I run the individual Simpletest-based test in phpstrom. I can easily run PHPUnit-based test in phpstrom by right click -> Run? Commented Mar 8, 2018 at 12:43
  • 1
    It's not supported natively but there have been attempts to make it work, see drupal.stackexchange.com/questions/203801/… and previousnext.com.au/blog/native-phpstorm-drupal-test-runner
    – Clive
    Commented Mar 8, 2018 at 13:24

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.