Home > Backend Development > PHP Tutorial > [PHP]PHPUnit入门之二

[PHP]PHPUnit入门之二

WBOY
Release: 2016-06-23 14:31:58
Original
954 people have browsed it

Data Providers

 

一个test method可以接受任意个参数。这些参数可以通过一个data provider method(下例中的provider())提供。
data provider method用@dataProvider来声明。
一个data provider method必须是public的,可以返回一组数组,也可以返回一个对象,该对象继承于Iterator接口,根据每一步迭代产生了一个数组。
对每个数组都是这个集合的一部分,test method调用时将以数组内容作为它的参数。

代码

 1 php
 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 <2><3>







Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template