Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssertionFailedError loses stacktrace in process isolation when chaining with a previous exception #5068

Closed
mondrake opened this issue Oct 2, 2022 · 4 comments
Labels
feature/process-isolation Issues related to running tests in separate PHP processes type/bug Something is broken

Comments

@mondrake
Copy link

mondrake commented Oct 2, 2022

Q A
PHPUnit version 9.5.24
PHP version n/a
Installation Method Composer

Summary

Identified in #4983.

When a AssertionFailedError is thrown as a re-throw of a previous exception (for instance as part of a onNotSuccessfulTest() callback), and the previous exception is chained in, the failure reported by PHPUnit traces to the part where AssertionFailedError was thrown, and the original trace is lost. So there is no information about where the test failed.

Current behavior

PHPUnit 9.5.24 #StandWithUkraine

Runtime:       PHP 8.1.10
Configuration: /home/runner/work/d8-unit/d8-unit/core/phpunit.xml.dist

Testing Drupal\Tests\node\Functional\NodeTitleTest
F                                                                   1 / 1 (100%)

Time: 00:06.500, Memory: 10.00 MB

There was 1 failure:

1) Drupal\Tests\node\Functional\NodeTitleTest::testNodeTitle
Title found

/home/runner/work/d8-unit/d8-unit/core/tests/Drupal/Tests/BrowserTestBase.php:441
/home/runner/work/d8-unit/d8-unit/core/tests/Drupal/Tests/Traits/UnsuccessfulTestHandlerTrait.php:16
/home/runner/work/d8-unit/d8-unit/vendor/phpunit/phpunit/src/Framework/TestResult.php:726

FAILURES!
Tests: 1, Assertions: 17, Failures: 1

How to reproduce

Implement a onNotSuccessfulTest() callback in a test, and add logic so that a Throwable is re-thrown as a AssertionFailedError with the Throwable chained in. For example,

    if ($t instanceof MinkException) {
      throw new AssertionFailedError($t->getMessage(), $t->getCode(), $t);
    }

Expected behavior

PHPUnit 9.5.24 #StandWithUkraine

Runtime:       PHP 8.1.10
Configuration: /home/runner/work/d8-unit/d8-unit/core/phpunit.xml.dist

Testing Drupal\Tests\node\Functional\NodeTitleTest
F                                                                   1 / 1 (100%)

Time: 00:06.500, Memory: 10.00 MB

There was 1 failure:

1) Drupal\Tests\node\Functional\NodeTitleTest::testNodeTitle
Title found

/home/runner/work/d8-unit/d8-unit/core/tests/Drupal/Tests/BrowserTestBase.php:441
/home/runner/work/d8-unit/d8-unit/core/tests/Drupal/Tests/Traits/UnsuccessfulTestHandlerTrait.php:16
/home/runner/work/d8-unit/d8-unit/vendor/phpunit/phpunit/src/Framework/TestResult.php:726

Caused by Behat\Mink\Exception\ExpectationException: Title found
/home/runner/work/d8-unit/d8-unit/core/tests/Drupal/Tests/WebAssert.php:538
/home/runner/work/d8-unit/d8-unit/core/tests/Drupal/Tests/WebAssert.php:281
/home/runner/work/d8-unit/d8-unit/core/modules/node/tests/src/Functional/NodeTitleTest.php:94
/home/runner/work/d8-unit/d8-unit/vendor/phpunit/phpunit/src/Framework/TestResult.php:726

FAILURES!
Tests: 1, Assertions: 17, Failures: 1.
@mondrake mondrake added the type/bug Something is broken label Oct 2, 2022
@mondrake
Copy link
Author

mondrake commented Oct 2, 2022

Will file a PR soon.

@mondrake mondrake changed the title AssertionFailedError loses original trace when chaining with a previous exceptions AssertionFailedError loses original trace when chaining with a previous exception Oct 2, 2022
@mondrake mondrake changed the title AssertionFailedError loses original trace when chaining with a previous exception AssertionFailedError in a @runInSeparateProcess context loses stack trace when chaining with a previous exception Oct 3, 2022
@mondrake
Copy link
Author

mondrake commented Oct 3, 2022

Running tests, I realized this occurs only when the test is executed in a separate process. Which is the case of Drupal functional tests where we found this issue. When executed within the same process, the stack trace is printed. Thanks @alexpott for pointing this out.

@sebastianbergmann sebastianbergmann changed the title AssertionFailedError in a @runInSeparateProcess context loses stack trace when chaining with a previous exception AssertionFailedError loses stacktrace in process isolation when chaining with a previous exception Oct 6, 2022
@sebastianbergmann sebastianbergmann added the feature/process-isolation Issues related to running tests in separate PHP processes label Oct 6, 2022
@sebastianbergmann
Copy link
Owner

I will leave this ticket open for now, but please note that I will not work on this as I do not think that this edge case is worth pursuing.

I strongly recommend to change your implementation of onNotSuccessfulTest() to not rely on exception chaining. AFACIS, all that needs to be done is mapping the information from Mink's ExpectationException to PHPUnit's AssertionFailedError.

@mondrake
Copy link
Author

mondrake commented Oct 6, 2022

Let's not waste time, then.

@mondrake mondrake closed this as completed Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/process-isolation Issues related to running tests in separate PHP processes type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

2 participants