PHPUnit-当dataProvider返回空数组时,不要失败 [英] PHPUnit - Don't fail when the dataProvider returns an empty array

查看:76
本文介绍了PHPUnit-当dataProvider返回空数组时,不要失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用@dataProvider的PHPUnit测试.数据提供程序检查文件系统中是否有某些文件.

I have a PHPUnit test which uses a @dataProvider. The dataprovider checks the filesystem for certain files.

但是,我在不同的环境中使用此测试,这意味着可能会发生文件不存在的情况.这意味着dataProvider找不到任何东西,并且不执行测试.

However I'm using this test in different environments what means it can happen that the files do not exist. This means that the dataProvider does not find anything and that the test is not executed.

这会导致测试运行以失败告终.

And this causes the test run to end in a failure.

问题:是否可以将PHPUnit配置为忽略那些提供程序什么都不产生的测试?

Question: is the a way to configure PHPUnit to ignore tests which have providers that produce nothing?

推荐答案

虽然我不知道任何phpunit选项(这并不意味着一个不存在),但是您可以使用如下所示的内容:

While I do not know of any phpunit option (which doesn't mean one doesn't exist), you could just 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,但是您可以进行调整以适合您的测试.

It's a workaround, sure, but it should stop an error from showing up. Obviously, whatever you do will depend on whether the first (or any arbitrary) argument is allowed to be false, but you can make tweaks to fit your tests.

这篇关于PHPUnit-当dataProvider返回空数组时,不要失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆