Skip to main content

One Test, Many Cases

Most functions need the same check for many inputs: normal values, negative values, zero, edge cases. Copying the test five times makes five places to maintain.

The PHPUnit way is a data provider: put the cases into one table — each row holds a name, the inputs and the expected output — and let the runner call one test method once per row.

Adding a new case becomes one new row in the table. This is the most common test pattern you will see in real PHPUnit code.


The Data Provider

The provider is a public static method that returns the table: an array of rows, keyed by the case name. The test method takes a row's values as parameters and is linked to the provider by the #[DataProvider('addCases')] attribute (imported from PHPUnit\Framework\Attributes).

PHPUnit calls the method once per row, and every row counts as its own test — a broken row fails alone, so every broken case shows up, not just the first.

Four cases in one table

Four rows, one test method, one assertion. All pass — the summary says OK (4 tests, 4 assertions). To test a fifth situation you would add one row to the table — the method stays untouched.

Prompt
Write a PHPUnit data-provider test for an add function: a static provider method returning rows keyed by a case name, each row with two int inputs and the expected result, covering one plus one, positive and negative, both negative, and zeros. Link the provider to one test method with the DataProvider attribute. Show the phpunit output.
Output: PHP script
Four cases in one table

Named Cases with --testdox

--testdox shows what the rows did: every case appears with its data set name, like Add with data set "both negative".

Each row passes or fails on its own, which makes a broken case easy to spot in a long table.

Every case gets a name

The same table, run with --testdox. The output now lists all four cases separately — you can see exactly which cases ran and passed.

Prompt
Write the same data-provider PHPUnit test for add and run it with --testdox so every case appears with its data set name. Show the output with all four case lines.
Output: PHP script

Run One Case Alone

Cases can be selected from the command line with --filter, using the data set name in quotes:

./vendor/bin/phpunit --testdox --filter '"both negative"' AddTest.php

Only the matching case runs. This is perfect while fixing one broken case: rerun just that case until it is green, then run everything again.

Filter one data set

The same file as above, run with --filter '"both negative"'. Only one of the four cases executes — the others are not even started: the summary says OK (1 test, 1 assertion).

Prompt
Using the data-provider PHPUnit test for add, show the output of running only one case with --testdox --filter '"both negative"' — only that data set should appear.
Output: PHP script