Drupal Core Resolves 14-Year Kernel Testing Issue With ShareEnvironment
After more than 14 years, Drupal core contributors have resolved issue #1411074, which proposed allowing kernel tests to share their test environment. The change introduces a PHP attribute called #[ShareEnvironment]. It was committed to the Drupal 12 development branch on 6 October 2026 and backported to Drupal 11 development branches on 8 October, when the issue was marked fixed.
Kernel tests check Drupal functionality using a limited application environment rather than a complete site installation. Until now, each test method typically required its own database initialisation, even when several methods in the same class needed identical setup. That repeated work can extend test runs, particularly for classes containing many methods.
According to the Drupal core change record, individual classes can opt into a shared environment by adding #[ShareEnvironment]. Classes without the attribute retain their previous behaviour in Drupal 12. A test class that needs a dedicated environment can instead declare #[ShareEnvironment(FALSE)].
The implementation adds setUpEnvironment() for setup tasks that run once per class and resetEnvironment() for cleanup between test methods. The existing setUp() method remains available for work that must run before each method. Developers adopting the attribute may therefore need to move repeated schema or configuration installation into the class-level setup method.
Sharing an environment does not mean sharing an entire running PHP process. Each test method still runs in a separate process, so object properties do not carry across methods, and site directories remain specific to individual tests. Only the database state is shared. Tests must not assume that tables are empty, that new records receive fixed identifiers or that methods execute in a particular order.
Performance measurements discussed during development suggest that avoiding repeated setup can help, but they do not establish a universal improvement across the core test suite. A separate container-reuse optimisation was committed to Drupal's main development branch on 10 October 2026. It builds on database sharing by avoiding repeated container compilation for compatible tests.
During local profiling of that follow-up, a contributor reported that the 26-method NodeTypeValidationTest completed in 15.179 seconds, compared with 19.419 seconds beforehand. The measurement concerned that specific test class and the separate container-reuse change; it is not a benchmark for all Drupal continuous integration jobs or for the #[ShareEnvironment] attribute on its own.
Further adoption is still under review. One follow-up issue proposes adding explicit shared-environment settings to all core kernel test classes, including opt-outs for incompatible tests. Another proposal would make sharing the default when the attribute is absent in Drupal 13. For contributed module maintainers, adoption remains optional under the currently documented behaviour and requires tests to handle retained database state correctly.
References
-
-
-
Re-use container in kernel tests | Drupal.org, Drupal.org
