Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can phpunit use multiple data provider

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

like image 378
imcoddy Avatar asked Feb 28 '13 00:02

imcoddy


People also ask

What is the use of PHPUnit?

PHPUnit is a framework independent library for unit testing PHP. Unit testing is a method by which small units of code are tested against expected results. Traditional testing tests an app as a whole meaning that individual components rarely get tested alone.

What is Dataprovider PHP?

Data provider is a function, that should return data for your particular test case. A data provider method must be public and either return an array of arrays or an object that implements the Iterator interface and yields an array for each iteration step.

Is PHPUnit a framework?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. The currently supported versions are PHPUnit 9 and PHPUnit 8.

What is PHPUnit?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit architecture for unit testing frameworks that originated with SUnit and became popular with JUnit. PHPUnit was created by Sebastian Bergmann and its development is hosted on GitHub.


2 Answers

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.

like image 177
Alexandre Borela Avatar answered Sep 18 '22 04:09

Alexandre Borela


You can use a helper function as shown below. The only problem is if the total number of test cases provided by all "sub data providers" is large, it can be tedious to figure out which test case is causing a problem.

/**   * @dataProvider allIds  */ public function testGetByIdUnsuccess($id) {        $this->assertNull($this->model->getById($id)); }    public function allIds() {     return array_merge(provideInvalidId(),provideFailedId()); } 
like image 33
Dwayne Towell Avatar answered Sep 21 '22 04:09

Dwayne Towell