Using providers in PHPUnit

So here’s a real simple example of how to use Providers in PHPUnit.

They’re very useful for running the exact same test multiple times but with different data being fed in.

class ScoringTest extends TestCase
{
/**
* Some Grades
*/
public static function all_grades_provider()
{
return [
[100.00],
[50.00],
[0],
];
}
/**
* Grade checks
* @dataProvider all_grades_provider
* @return void
*/
public function test_grade_score_is_correct ($grade)
{
// test our total is correct
$this->assertEquals($grade, Score::someFunctionWhichAffectsGrade($grade));
}
}

view raw
gistfile1.php
hosted with ❤ by GitHub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s