2

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?

2

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.

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.