将数据提供者传递给 PHPUnit 中的 setUp() [英] Passing data provider to setUp() in PHPUnit

查看:55
本文介绍了将数据提供者传递给 PHPUnit 中的 setUp()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将数据从我的数据提供者传递到 PHPUnit 中的 setUp() 方法.

I'm currently trying to pass data from my data provider to the setUp()-method in PHPUnit.

背景:我使用 PHPUnit 在不同浏览器中运行前端测试.浏览器应该在数据提供者内部定义,并且需要通过 setUp() 方法知道.

Background: I am using PHPUnit for running frontend-tests in different browsers. The browser should be defined inside the data provider and needs to be known by the setUp()-method.

我了解,数据提供程序最初是在调用 setUp() 方法(如 setUpBeforeClass())之前执行的.因此 setUp()-data 不能传递给数据提供者.但它应该反过来工作,不是吗?

I understand, that a data provider initially is executed before the setUp()-method (as setUpBeforeClass()) is called. Therefore setUp()-data can not be passed to a data provider. But it should work the other way round, shouldn't it?

PHPUnit 是否使用集成"的数据提供者的数据生成自己的临时测试类?

Does PHPUnit generate its own temporarily testclasses with data from the data provider "integrated"?

当然:解决方法可能是再次读取 setUp() 方法中的 XML 文件.但这是最后的选择,我会考虑......

Of course: a workaround could be, to read the XML-file in the setUp()-method again. But that's the last option, I'd consider...

提供了一个小片段:

dataProvider() 的一部分:

part of dataProvider():

public function dataProvider()
{
    $this->xmlCnf = $data['config'];
    var_dump($this->xmlCnf); // array with config is exposed
    // [...]
}

还有 setUp() 方法:

And the setUp()-method:

 protected function setUp()
{
    var_dump($this->xmlCnf); // NULL
    //[...]
}

推荐答案

如果这对任何人有用:

以下代码应该可以工作:

The following code should work:

public function dataProvider()
{
    return [ [ /* dataset 1 */] , ... ]
}

protected setUp() {
    parent::setUp();
    $arguments = $this->getProvidedData();
    // $arguments should match the provided arguments for this test case
}

/** 
 * @dataProvider dataProvider
 */
public function testCase(...$arguments) {

}

getProvidedData 方法似乎从 PHPUnit 5.6 开始就可用了(在最初提出这个问题之前或之后不久)

The getProvidedData method seems to have been available since PHPUnit 5.6 (which was either shortly before or after this question was originally asked)

这篇关于将数据提供者传递给 PHPUnit 中的 setUp()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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