Can phpunit use multiple data provider

imcoddy picture imcoddy · Feb 28, 2013 · Viewed 11.4k times · Source

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

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.

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.

So the code goes like this:

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

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: did not find too much help in here

Answer

Alexandre Borela picture Alexandre Borela · Oct 9, 2016

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));
}

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