Skip to content

PHPUnit executes the teardown method even if the test was skipped during setup #6394

@morozov

Description

@morozov
Q A
PHPUnit version 11.5.32...42
PHP version 8.4.13
Installation Method Composer

Summary

Some Doctrine DBAL tests are skipped in the setup, if it's detected that the current runtime environment or test configuration are unsuitable for the test.

If the test is skipped during the setup, it is expected that the teardown procedure is also skipped, because it depends on the same runtime environment and/or configuration which don't satisfy the test.

Prior to #6281, if a test was skipped during setup, the corresponding teardown method wasn't executed. Now it's executed.

How to reproduce

<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class PHPUnitTest extends TestCase
{
    protected function setUp(): void
    {
        self::markTestSkipped();
    }

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

    public function testPHPUnit(): void
    {
        self::assertTrue(true);
    }
}

Expected behavior

➜ phpunit tests/Functional/PHPUnitTest.php
PHPUnit 11.5.23 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.4.13
Configuration: /Users/morozov/Projects/dbal/phpunit.xml.dist

S                                                                                               1 / 1 (100%)

Time: 00:00.011, Memory: 10.00 MB

OK, but some tests were skipped!
Tests: 1, Assertions: 0, Skipped: 1.

Current behavior

➜ phpunit tests/Functional/PHPUnitTest.php
PHPUnit 11.5.32 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.4.13
Configuration: /Users/morozov/Projects/dbal/phpunit.xml.dist

S                                                                                               1 / 1 (100%)E

Time: 00:00.005, Memory: 10.00 MB

There was 1 error:

1) Doctrine\DBAL\Tests\Functional\PHPUnitTest::testPHPUnit
Exception:

/Users/morozov/Projects/dbal/tests/Functional/PHPUnitTest.php:21

ERRORS!
Tests: 2, Assertions: 0, Errors: 1, Skipped: 1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions