PHPUnit - not crashing when dataProvider returns an empty array

I have a PHPUnit test that uses @dataProvider. Dataprovider checks the file system for specific files.

However, I use this test in different environments, which means that the files do not exist. This means that dataProvider does not find anything and that the test is not running.

And this leads to the completion of the trial run.

Question: is there a way to configure PHPUnit to ignore tests that have providers that do not produce anything?

+4
source share
2 answers

As long as I don’t know any phpunit option (this does not mean that it does not exist), you can simply use something like the following:

public function providerNewFileProvider()
{
    //get files from the "real" provider
    $files = $this->providerOldFileProvider();
    //no files? then, make a fake entry to "skip" the test
    if (empty($files)) {
        $files[] = array(false,false);
    }

    return $files;
}

/**
* @dataProvider providerNewFileProvider
*/

public function testMyFilesTest($firstArg,$secondArg)
{
    if (false === $firstArg) {
        //no files existing is okay, so just end immediately
        $this->markTestSkipped('No files to test');
    }

    //...normal operation code goes here
}

, , . , , , , ( ) false, .

+4

, , , .

public function fileDataProvider()
{
    $files = $this->getTestFiles();
    if (empty($files)) {
        $this->markTestSkipped('No files to test');
    }

    return $files;
}

/**
 * @dataProvider fileDataProvider
 */

public function testMyFilesTest($firstArg, $secondArg)
{
    //...normal operation code goes here
}
+3

Source: https://habr.com/ru/post/1541944/


All Articles