PHPUnit testing in Drupal 8 works for functionality which are written in class files.

How do I test the methods or hooks implemented in a .module file using PHPUnit in Drupal 8?


No, PHPUnit is not for testing functionalities written in class files. Quite the opposite, PHPUnit in Drupal can be used to test hooks, functions, and classes implemented from modules.
Drupal core itself has tests for hooks, for example the InfoAlterTest class that tests hook_system_info_alter().

How to write tests is described in Automated tests. Without a specific use-case, I cannot give more details on testing hooks, except saying that functional tests that do not test web output, which is the case for most of the hook tests, the test class needs to extend \Drupal\KernelTests\KernelTestBase.


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.