Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

If you want to see sensible error messages with labels for broken unit tests where Data Providers are used, you can follow the second test case below. Check the "Test result" sections for differences.


Data Providers without labels


class SomethingTest extends PHPUnit_Framework_TestCase
{
/**
* @test
*
* @dataProvider getDataProvider
*/
public function it_does_something(
$param1,
$param2
) {
// ...
}

public function getDataProvider()
{
return [
['Hello', 11],
['World', 22],
];
}
}

Test result


1) SomethingTest::it_does_something with data set #0 ('Hello', 11)
Failed asserting that false matches expected true.
2) SomethingTest::it_does_something with data set #1 ('World', 22)
Failed asserting that false matches expected true.

Data Providers with labels


class SomethingTest extends PHPUnit_Framework_TestCase
{
/**
* @test
*
* @dataProvider getDataProvider
*/
public function it_does_something(
$param1,
$param2
) {
// ...
}

public function getDataProvider()
{
return [
'Testing Hello message' => [
'$param1' => 'Hello',
'$param2' => 11
],
'Testing World message' => [
'$param1' => 'World',
'$param2' => 22
],
];
}
}

Test result


1) SomethingTest::it_does_something with data set "Testing Hello message" ('Hello', 11)
Failed asserting that false matches expected true.
2) SomethingTest::it_does_something with data set "Testing World message" ('World', 22)
Failed asserting that false matches expected true.