phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

Allow enabling process isolation on suite/test base class

Open mondrake opened this issue 1 year ago • 2 comments

Process isolation can only be set via attributes in test classes (either overall on a class or on a single method) or on execution level (with attribute in XML or as a command line argument).

It would be good to enable process isolation on a middle ground, for instance for an entire suite or at a test base class level.

Drupal completed support of PHPUnit 10 in https://www.drupal.org/project/drupal/issues/3417066, and we needed there to find a workaround for this - there are 5 test suites in configuration, 3 of which have tests that need to run in process isolation, the others don't.

Since for each test suite there's a base test class extending from TestCase, we resolved by extending the constructor and setting process isolation there:

  public function __construct(string $name) {
    parent::__construct($name);
    $this->setRunTestInSeparateProcess(TRUE);
  }

https://git.drupalcode.org/project/drupal/-/blame/11.x/core/tests/Drupal/KernelTests/KernelTestBase.php?ref_type=heads#L107-L113

Prior to PHPUnit 10, this was possible by overriding TestCase properties, but this is no longer possible. The workaround works, but extends a constructor marked @internal and we are exposed to potential changes.

mondrake avatar May 19 '24 10:05 mondrake

Would it be possible to look for class level attributes in parent classes as well? Then we could specify #[RunTestsInSeparateProcesses] on the base class and it would automatically apply to all subclasses.

longwave avatar May 23 '24 16:05 longwave

Bumping this in the hope of a solution to enabling isolation from a base class. After upgrading to PHPUnit 11 we now receive a new warning and it looks like our current solution will be closed off in PHPUnit 12:

The "PHPUnit\Framework\TestCase::__construct()" method is considered final. It may change without further notice as of its next major version. You should not extend it from "Drupal\Tests\BrowserTestBase".

Is there a supported way to enable isolation for a set of tests, other than adding the attribute to every test class?

longwave avatar Jan 03 '25 12:01 longwave