Description
In the Drupal project we are using Mink to drive emulated web browsers and perform assertions against web content. Mink throws its own exception when an assertion fails, and by default PHPUnit catches this and reports it as an error:
PHPUnit 9.5.20 #StandWithUkraine
Testing Drupal\Tests\node\Functional\NodeTitleTest
E 1 / 1 (100%)
Time: 00:07.227, Memory: 4.00 MB
There was 1 error:
1) Drupal\Tests\node\Functional\NodeTitleTest::testNodeTitle
Behat\Mink\Exception\ExpectationException: Title found
/var/www/html/drupal/core/tests/Drupal/Tests/WebAssert.php:540
/var/www/html/drupal/core/tests/Drupal/Tests/WebAssert.php:283
/var/www/html/drupal/core/modules/node/tests/src/Functional/NodeTitleTest.php:94
/var/www/html/drupal/vendor/phpunit/phpunit/src/Framework/TestResult.php:726
ERRORS!
Tests: 1, Assertions: 17, Errors: 1.
However, we would like these to be treated as assertion failures instead of errors. When presented with multiple test failures, it is usually easier to tackle the real errors first, and so being able to identify assertion failures separately is a useful feature.
Is it possible to add a feature to PHPUnit where specific classes of exceptions can be reported as failures? I have already tried catching the exception and throwing a new AssertionFailedError
from onNotSuccessfulTest()
but this loses the stack trace, which is critical to debugging a test fail, so it would be helpful if this feature could be configured in PHPUnit directly.
The Drupal issue where this is being discussed is https://www.drupal.org/project/drupal/issues/3271214