Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with PHPUnit and Data Providers

I have the following test case:

include_once('../Logger.php');

class LoggerTest extends PHPUnit_Framework_TestCase {

    public function providerLogger() {
        return new Logger;
    }

    /**
     * @dataProvider providerLogger
     */
    public function testAddStream($logger) {
        $this->assertTrue(false);
    }

}

When I run it in PHPUnit, I get:

PHPUnit 3.4.14 by Sebastian Bergmann.

..........

Time: 0 seconds, Memory: 5.75Mb

OK (1 tests, 0 assertions)

Test should fail, but it doesn't. I tried having:

public function providerLogger() {
    return array(new Logger);
}

But I get:

The data provider specified for LoggerTest::testAddStream is invalid.

I tried declaring it static (like the manual says), but still no difference.

I remember having it working in a similar fashion before, but I could be wrong. What am I missing?

Thanks in advance for your help.

PHPUnit 3.4.14 (taken from PEAR) on PHP 5.3.3

like image 228
netcoder Avatar asked Nov 24 '10 00:11

netcoder


People also ask

What is PHPUnit used for?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.

Is PHPUnit open source?

#3) PHPUnitIt is an open source testing tool used for PHP code. It is the most widely used framework for unit testing.

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 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.


3 Answers

Minor update: It's OK to use instance methods as provider since version 3.2 (or somewhere around that). Have a look at the comments


The provider must look like this.

public static function providerLogger() {
    return array(
      array(new Logger)
    );
}

First of all: The method must be static if you are using phpunit version lower than 3.3 .

The array s are important. Its not that hard to understand. The outer array has one value for each iteration the test should get called. Here the test just get called once. The inner arrays are the parameters (in order) the test is invoked with. Your test expects exactly one parameter, so the inner arrays always needs exactly one value. Another little example

public static function addTestProvider () {
    return array(
        /* First + Second = third? */
        array(1,4,5),
        array(3,3,6),
        array(5,5,6)
    );
}
public function testAdd ($a, $b, $result) {
    $this->assertEquals($result, $a + $b);
}

Here testAdd gets executed 3 times, one for every second-level array, and it will receive the values from the inner array s. You may notice, that the test will fail and provides you a message in which iteration of the dataset (here #3, because 5+5 is not 6 ;)) the assertion failed.

like image 83
KingCrunch Avatar answered Oct 01 '22 16:10

KingCrunch


I had the same problem, and it was solved, when i deleted the empty constructor,that was auto generated. Iam not sure why this solves the problem. I also had no test method named like the class. The provider method doesnt need to be static, so far my test run without static. But also run when i make the provider method static

like image 40
Christian Steinmann Avatar answered Oct 01 '22 15:10

Christian Steinmann


<?php

require_once 'calculator.php';

/**
 * Calculator test case.
 */
class CalculatorTest extends PHPUnit_Framework_TestCase {

    /**
     * @var Calculator
     */
    private $Calculator;

    /**
     * Prepares the environment before running a test.
     */
    protected function setUp() {
        parent::setUp ();
        // TODO Auto-generated CalculatorTest::setUp()
        $this->Calculator = new Calculator(/* parameters */);
    }

    /**
     * Cleans up the environment after running a test.
     */
    protected function tearDown() {
        // TODO Auto-generated CalculatorTest::tearDown()
        $this->Calculator = null;
        parent::tearDown ();
    }

    /**
     * Constructs the test case.
     */
    public function __construct() {
        // TODO Auto-generated constructor
    }

    /**
     * Tests Calculator->add()
     *
         * @dataProvider provider
         */
    public function testAdd($a, $b, $c) {
        // TODO Auto-generated CalculatorTest->testAdd()
        //$this->markTestIncomplete ( "add test not implemented" );

        //$this->Calculator->add(/* parameters */);
        $this->assertEquals($this->Calculator->add($a, $b), $c);
    }

    public static function provider()
    {
        return array(
          array(1, 1, 1),
          array(1, 1, -1),
          array(4, 2, 2),
          array(1, 1, 1)
        );
    }
}

is the complete set of code

like image 1
Karkotagan Avatar answered Oct 01 '22 16:10

Karkotagan