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

Exceptions raised in TestCase::tearDown() are not handled correctly #5210

Closed
adalessa opened this issue Feb 18, 2023 · 2 comments
Closed

Exceptions raised in TestCase::tearDown() are not handled correctly #5210

adalessa opened this issue Feb 18, 2023 · 2 comments
Assignees
Labels
type/bug Something is broken version/10 Something affects PHPUnit 10

Comments

@adalessa
Copy link

Q A
PHPUnit version 10.0.7
PHP version 8.2.2
Installation Method Composer

Summary

Any exception thrown during the tear down is not reflected as an error on the test result nor the test is mark as failed.
I detected this due the use of the tear down to perform the check for Mockery with the Mockery::close() even when I expected the test to fail, the test is still passing which is wrong. After investigating detected that is not a mockery issue since is throwing the exception as expected but phpunit is not reporting the error.

Current behavior

Test are passing even when during the tearDown of the test an exception is throw.

How to reproduce

Create a simple test which during the tear down throw an exception

    public function test_it_should_fail(): void
    {
        $this->assertTrue(true);
    }

    protected function tearDown(): void
    {
        throw new \Exception('test');
    }

Expected behavior

The tests should fail with the message provided by the exception.

@adalessa adalessa added type/bug Something is broken version/10 Something affects PHPUnit 10 labels Feb 18, 2023
@sebastianbergmann
Copy link
Owner

I can reproduce this:

Test.php

<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class Test extends TestCase
{
    public function testOne(): void
    {
        $this->assertTrue(true);
    }

    protected function tearDown(): void
    {
        throw new \Exception('test');
    }
}

PHPUnit 10.0.7

PHPUnit 10.0.7 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.3

.                                                                   1 / 1 (100%)

Time: 00:00.076, Memory: 6.00 MB

OK (1 test, 1 assertion)

PHPUnit 9.6.3

PHPUnit 9.6.3 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 00:00.020, Memory: 6.00 MB

There was 1 error:

1) Test::testOne
Exception: test

/home/sb/Test.php:13

ERRORS!
Tests: 1, Assertions: 1, Errors: 1.

By the way: tearDown() should only be used to clean up after a test. You probably want to use the assertPostConditions() method instead for your use case.

@sebastianbergmann sebastianbergmann self-assigned this Feb 18, 2023
@sebastianbergmann sebastianbergmann added feature/test-runner CLI test runner and removed feature/test-runner CLI test runner labels Feb 18, 2023
sebastianbergmann added a commit that referenced this issue Feb 18, 2023
@adalessa
Copy link
Author

Thanks for the quick response. I understand the point of using assertPostConditions I have seen the use of the teardown is recommended on the mockery documentation and also is implemented in this way in Laravel. I of course understand that is not up to phpunit to determine how others will use it, I am just providing context of what I found during the investigation of this issue.

@sebastianbergmann sebastianbergmann changed the title TearDown Exceptions not registering as a failure for the test. Exceptions raised in TestCase::tearDown() are not handled correctly Feb 18, 2023
sebastianbergmann added a commit that referenced this issue Feb 19, 2023
This reverts commit 205444f.
sebastianbergmann added a commit that referenced this issue Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something is broken version/10 Something affects PHPUnit 10
Projects
None yet
Development

No branches or pull requests

2 participants