[PHP] PHPUnit entry 2

Source: Internet
Author: User
[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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.