How to unit test hook_form_alter implementation code in custom module?


Don't, that doesn't make sense. Unit tests make sense for APIs and classes, things with clear interfaces and definitions what's the input/output.

A form alter means you're altering more or less internal structures from another module that might change over time. Especially backend forms are explicitly excluded from Drupal Core BC for example, which means that they can be changed over time. You want to do a full browser test to make sure that your logic/changes actually work in the resulting form that is displayed in the browser.

See https://www.drupal.org/docs/8/testing/types-of-tests-in-drupal-8 for documentation of different types of tests in Drupal 8 and when they should be used. There are 4 different types of test classes for a reason.

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.