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

Filter tests based on the PHP extensions they require #5928

Closed
sebastianbergmann opened this issue Aug 20, 2024 · 0 comments
Closed

Filter tests based on the PHP extensions they require #5928

sebastianbergmann opened this issue Aug 20, 2024 · 0 comments
Assignees
Milestone

Comments

@sebastianbergmann
Copy link
Owner

Consider this test class:

<?php declare(strict_types=1);
namespace example;

use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;

#[RequiresPhpExtension('mysqli')]
final class ExampleTest extends TestCase
{
    public function testSomething(): void
    {
        // ...
    }
}

When the test suite that contains the test case shown above is run then the output shown below is printed when the mysqli extension is not available in the PHP process that runs PHPUnit and the tests:

There was 1 skipped test:

1) example\ExampleTest::testSomething
PHP extension mysqli is required.

The test is skipped as its requirements are not met. This is better than triggering PHP errors related to unknown symbols such as classes or functions.

The RequiresPhpExtension attribute is commonly used in projects that have optional functionality that requires a PHP extension to work. When this extension is available then the tests are run, when the extension is not available then the tests are skipped.

It is sometimes desirable to have separate continuous integration jobs that use different PHP configurations where, for example, some extensions are available or not available. In certain scenarios it would be nice to filter tests based on the PHP extensions they require.

Proposal

Implement a CLI option named --requires-php-extension mysqli for PHPUnit's test runner that only runs tests that have a #[RequiresPhpExtension('mysqli')] attribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant