Skip to content

expectProcessExit() #6234

Open
Open
@sebastianbergmann

Description

@sebastianbergmann

When we have this code ...

ProcessExit.php

<?php declare(strict_types=1);
final class ProcessExit
{
    public function doSomething(): never
    {
        exit(1);
    }
}

ProcessExitTest.php

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

#[CoversClass(ProcessExit::class)]
final class ProcessExitTest extends TestCase
{
    #[RunInSeparateProcess]
    public function testOne(): void
    {
        (new ProcessExit)->doSomething();
    }
}

and run PHPUnit then we currently get

PHPUnit 12.2.1 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.4.8
Configuration: /tmp/example/phpunit.xml

E                                                                   1 / 1 (100%)

Time: 00:00.156, Memory: 25.68 MB

There was 1 error:

1) ProcessExitTest::testOne
Test was run in child process and ended unexpectedly

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

Due to #[RunInSeparateProcess], the test method is run in a separate process and the exit statement does not terminate the PHP process in which PHPUnit's test runner is running.

The child process that is used to run the test ends unexpectedly and this is interpreted by PHPUnit as an error.

There are situations, though, when the child process using exit is expected. However, this cannot be expected currently.

The following should be made possible:

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

#[CoversClass(ProcessExit::class)]
final class ProcessExitTest extends TestCase
{
    #[RunInSeparateProcess]
    public function testOne(): void
    {
        $this->expectProcessExit(1);

        (new ProcessExit)->doSomething();
    }
}

Metadata

Metadata

Labels

feature/assertionIssues related to assertions and expectationstype/enhancementA new idea that should be implemented

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions