phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

Data Providers and Skipped Tests

Open kevinrsoursib opened this issue 1 year ago • 2 comments

PHPUnit 10.5 tightened up the data providers to flag as a error if a data provider returns an empty set. This causes problems in the case where the test the data provider feeds isn't something that is able to run in every situations where the tests are run. I'll use the example of a test of a feature that relies on a PHP module which may be absent.

If the module isn't there we know when we are generating the data provider records that we're just going to skip the test entirely. There really isn't much point in generating a long list of records just to skip the test dozens of times (along with needlessly spamming the output). This is much more of a problem if generating the data is time consuming or requires the absent module (for instance the module defines constants that are needed as part of the test data sets).

Previously we could do something like:

if (class_exists('module_class_name')) { return []; }

and things would work out fine. Now the only option appears to be generate the dataset regardless (if that's even possible) or generate a dummy dataset when the test conditions aren't met. Which isn't exactly hard, but it's an ugly workaround.

If we don't want to allow empty data sets, some way of marking the data provider as "skipped" in the same way as we would mark a test as skipped would be useful to avoid dummy data we aren't going to do anything with.

something like if (class_exists('module_class_name')) { self::markTestSkipped('Requires Module'); }

kevinrsoursib avatar Oct 22 '24 21:10 kevinrsoursib

the example of a test of a feature that relies on a PHP module which may be absent.

If the module isn't there we know when we are generating the data provider records that we're just going to skip the test entirely.

@kevinrsoursib this sounds to me like you want to use RequiresPhpExtension.

class Issue6011Test extends TestCase
{
    #[RequiresPhpExtension('I_DO_NOT_EXIST')]
    #[DataProvider('provideSomethingCases')]
    public function testSomething(): void
    {
        $this->fail('This should not be reached');
    }

    public static function provideSomethingCases(): array
    {
        return [];
    }
}

This results with:

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

kubawerlos avatar Oct 23 '24 05:10 kubawerlos

That could actually work (my use case is a little different but I think it could be handled similarly). I wouldn't have guessed from the documentation that that annotating the test function would affect the data provider though.

kevinrsoursib avatar Oct 23 '24 16:10 kevinrsoursib