phpunit可以使用多个数据提供程序吗 [英] Can phpunit use multiple data provider

查看:67
本文介绍了phpunit可以使用多个数据提供程序吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,一个问题:运行测试时phpunit可以使用多个数据提供程序吗?

One question in short: can phpunit use multiple data provider when running test?

例如,我有一个名为getById的方法,并且需要为此运行成功和不成功的测试用例.

For example, I have a method called getById, and I need to run both successful and unsuccessful testcases for it.

成功的测试用例意味着它可以返回相应的记录.对于不成功,输入可以分为两类:无效和失败.

The successful testcases means that it can return a corresponding record. And for the unsuccessful, the input can fall in two categories: invalid and failed.

无效意味着输入不合法,而失败意味着输入可能有效,但是没有具有该ID的相应记录.

The invalid means that the input is not legal, while failed means the input could be valid, but there is no corresponding record with that ID.

所以代码如下:

/** 
 * @dataProvider provideInvalidId
 * @dataProvider provideFailedId
 */
public function testGetByIdUnsuccess($id)
{   
    $this->assertNull($this->model->getById($id));
}   

但事实证明,仅使用了第一个数据提供者,而忽略了第二个.尽管我不确定该senario是否常见,但这是问题所在.我们可以使用多个数据提供程序吗?如果可以的话,怎么办?

But it turned out that only the first data provider has been used, ignoring the second one. Though I am not sure this senario is common or not, but here is the question. Can we use multiple data provider? And if we can, how?

PS:在推荐答案

只需对问题进行更新,即

Just an update to the question, a pull request was accepted and now the code:

/** 
 * @dataProvider provideInvalidId
 * @dataProvider provideFailedId
 */
public function testGetByIdUnsuccess($id)
{   
    $this->assertNull($this->model->getById($id));
}

在PHPUnit 5.7上可以使用,您将可以添加任意数量的提供程序.

Will work on PHPUnit 5.7, you'll be able to add as many providers as you want.

这篇关于phpunit可以使用多个数据提供程序吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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