Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parametrized tests in PHPUnit

On JUnit, you can use the annotation @RunWith(Parameterized.class) to run a single unit test several times with different actual and expected results. I'm new to PHPUnit so I would like to know which are suggested approachs for achieving the same (running one unit test with many actual, expected results)?

like image 927
ivansabik Avatar asked Mar 10 '14 15:03

ivansabik


People also ask

What is PHPUnit testing?

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.

What are parameterized tests?

Test parameterization is a type of data-driven testing that allows you to execute the same test, multiple times using different parameters. Xray Cloud has a parameterized tests feature, that executes the same test with different input values, multiple times, without ever having to clone or replicate it.

What is assertion in PHPUnit?

The assertEquals() function is a builtin function in PHPUnit and is used to assert whether the actual obtained value is equals to expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false.


1 Answers

You can use a so called data provider. Like this:

/**
 * @dataProvider providerPersonData
 */
public function testPerson($name, $age) {
    // test something ...
}

public function providerPersonData() {
    // test with this values
    return array(
        array('foo', 36),
        array('bar', 99),
        // ...
    );
}

You define the data provider using the @dataProvider annotation.

like image 85
hek2mgl Avatar answered Sep 21 '22 05:09

hek2mgl