@scotty_walker
Data providers in PHPUnit allow you to run a test method multiple times with different sets of data. Here's how you can use data providers in PHPUnit tests:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class MyTest extends PHPUnitFrameworkTestCase { /** * @dataProvider additionProvider */ public function testAddition($a, $b, $expected) { $result = $a + $b; $this->assertEquals($expected, $result); } public function additionProvider() { return [ [2, 3, 5], [0, 0, 0], [-3, -5, -8], ]; } } |
In the example above, the testAddition
method will be executed three times, once for each data set in the additionProvider
data provider method. It will perform the addition operation with the provided $a
and $b
values and assert that the result matches the expected value.
By using data providers, you can easily test multiple cases with different input values without duplicating code or writing separate test methods for each case.