Data Providers in PHPUnit 3.2

Sebastian Bergmann » 14 October 2007 » in New Features » 4 Comments

Starting with version 3.2, PHPUnit has support for TestNG-style data providers:
<?php
class DataTest extends PHPUnit_Framework_TestCase
{
    protected static $myData = array(
      array(0, 0, 0),
      array(0, 1, 1),
      array(1, 0, 1),
      array(1, 1, 3)
    );
 
    public static function provider()
    {
        return self::$myData;
    }
 
    /**
     * @dataProvider provider
     */
    public function testAdd($a, $b, $c)
    {
        $this->assertEquals($c, $a + $b);
    }
}
?>
The method specified by the @dataProvider annotation returns an array or an Iterator object that contain argument arrays. For each of these arrays a test object is created that will run the test method with the argument from the array:
sb@vmware ~ % phpunit DataTest
PHPUnit 3.2.0-dev by Sebastian Bergmann.

...F

Time: 0 seconds

There was 1 failure:

1) testAdd(DataTest) with data (1, 1, 3)
Failed asserting that <integer:2> matches expected value <integer:3>.
/home/sb/DataTest.php:21

FAILURES!
Tests: 4, Failures: 1.
Defined tags for this entry: , ,

Trackback specific URI for this entry

4 Comments to "Data Providers in PHPUnit 3.2"

Display comments as (Linear | Threaded)
  1. Toby
    14/10/2007 at 18:50 Permalink
    Sounds like a really usable feature, I already have several places in mind where it will make testing a lot easier. :)

    Thanks, Sebastian, for your amazing work on this project!

    Reply

  2. Olle Jonsson
    16/10/2007 at 19:21 Permalink
    I was impressed that this thing was introduced on the first page of "how to write tests". This is a recent feature, and it's already in the documentation.

    Very enjoyable. Thanks! I'm warming up to using PHPUnit. Very warm.

    Reply

  3. Song
    13/06/2008 at 11:46 Permalink
    Hello, I found that this feature doesn`t work in 3.2.20. I don`t know if other version of PHPUnit works.

    The @dataProvider annotation only be processed in PHPUnit_Util_Test::getProvidedData, and this method only be called when construct a TestCase with parameters in PHPUnit_Framework_TestSuite::createTest. I think this should be a bug.

    Reply

  4. Sebastian Bergmann
    14/06/2008 at 14:18 Permalink
    Please do not report bugs on the blog but use the issue tracker instead. Thanks!

    Reply

3 Trackbacks to "Data Providers in PHPUnit 3.2"

  1. Sebastian Bergmann 28/10/2007 at 18:29
    I have just released a first release candidate for PHPUnit 3.2, the next major version of the xUnit testing framework for PHP. Highlights of this new release include software metrics, project mess detection, copy-and-paste detection, improvements to Se
  2. Sebastian Bergmann 12/11/2007 at 10:21
    I have just released a second release candidate for PHPUnit 3.2, the next major version of the xUnit testing framework for PHP. Highlights of this new release include software metrics, project mess detection, copy-and-paste detection, improvements to S
  3. Sebastian Bergmann 19/11/2007 at 12:06
    Sebastian Bergmann and his contributors are proud to announce the immediate availability of PHPUnit 3.2. This release is a major improvement to the popular open source testing solution for PHP applications. It includes new features and bug fixes. High

Add Comment


To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Submitted comments will be subject to moderation before being displayed.