[PHP] PHPUnit entry 2 Data Providers
A test method can accept any parameter. These parameters can be provided through a data provider method (provider () in the following example.
The data provider method is declared by @ dataProvider.
A data provider method must be public. it can return an array or an object that inherits from the Iterator interface and generates an array based on each iteration.
Each array is part of this set. the test method uses the array content as its parameter when calling.
Code
1 2 class DataTest extends PHPUnit_Framework_TestCase
3 {
4 /**
5 * @ dataProvider provider
6 */
7 public function testAdd ($ a, $ B, $ c)
8 {
9 $ this-> assertEquals ($ c, $ a + $ B );
10}
11
12 public function provider ()
13 {
14 return array (
15 array (0, 0, 0 ),
16 array (0, 1, 1 ),
17 array (1, 0, 1 ),
18 array (1, 1, 3)
19 );
20}
21}
22?>
Phpunit DataTest
PHPUnit 3.4.2 by Sebastian Bergmann.
... F
Time: 0 seconds
There was 1 failure:
1) testAdd (DataTest) with data (1, 1, 3)
Failed asserting that Matches expected value .
/Home/sb/DataTest. php: 21
FAILURES!
Tests: 4, Assertions: 4, Failures: 1.
In the preceding example, the fourth data group fails to pass the test.
* When a test accepts parameters from both the @ dataProvider method and one or more @ depends methods, the parameters from data provider are before those from @ depends.