Assertions
Learn common assertions like assertEquals, assertTrue and assertContains.
assertEquals and assertSame
assertEquals($expected, $actual) is the most common assertion. It compares two values with ==; if equal, the test passes. assertSame also checks the type (===): assertSame(4, 2 + 2) passes but assertSame(4, "4") fails. assertEquals(4, "4") passes because == is loose. Which to pick depends on what the test means.
Other common assertions
assertTrue($x) expects the value to be true; assertFalse the opposite. assertContains("Apple", $list) checks that an item is in an array. assertCount(3, $list) checks the array length. PHPUnit offers more than 50 assertions; all share the same idea: if the condition fails, the test fails. Pick the most meaningful: assertGreaterThan(0, $x) reads better than assertTrue($x > 0).
Exercise Write multiple assertions
The ScoreKeeper class adds scores and returns the average. Make ScoreKeeperTest extend TestCase. In testStats add 10 and 20, then check the sum is 30 with assertEquals, the list has 2 items with assertCount, and the average is greater than 10 with assertTrue.
Show example solution
Write class ScoreKeeperTest extends TestCase. In testStats add $sk = new ScoreKeeper(); $sk->add(10); $sk->add(20); $this->assertEquals(30, array_sum($sk->scores)); $this->assertCount(2, $sk->scores); $this->assertTrue($sk->average() > 10);
Example solution:
<?php
// Here we define TestCase ourselves
class TestCase {
protected function assertEquals($expected, $actual): void {
if ($expected !== $actual) {
throw new Exception("Expected $expected, got $actual");
}
}
protected function assertTrue($condition): void {
if ($condition !== true) { throw new Exception("Condition is not true"); }
}
protected function assertCount($expected, $actual): void {
if (count($actual) !== $expected) {
throw new Exception("Expected count $expected, got " . count($actual));
}
}
}
class ScoreKeeper {
public array $scores = [];
public function add(int $p): void { $this->scores[] = $p; }
public function average(): float {
return count($this->scores) ? array_sum($this->scores) / count($this->scores) : 0;
}
}
class ScoreKeeperTest extends TestCase {
public function testStats(): void {
$sk = new ScoreKeeper();
$sk->add(10);
$sk->add(20);
$this->assertEquals(30, array_sum($sk->scores));
$this->assertCount(2, $sk->scores);
$this->assertTrue($sk->average() > 10);
}
}
// Run the test
$test = new ScoreKeeperTest();
$test->testStats();
echo "testStats: PASSED\n";
Test yourself
-
Match the assertions
- assertEquals
- Value equality
- assertSame
- Value and type
- assertTrue
- Expects true
- assertContains
- In array
-
Which assertion treats int 4 and string "4" as different?
- assertEquals(4, "4")
- assertEquals("4", "4")
- assertSame(4, "4")
- assertTrue(4 == "4")
Show answer
assertSame(4, "4") — Correct. assertSame also checks the type; int 4 and string "4" are not the same.
-
Complete the assertTrue call
<?php public function testIsAdult(): void { $user = new User(25); $this->____($user->isAdult()); }Show answer
<?php public function testIsAdult(): void { $user = new User(25); $this->assertTrue($user->isAdult()); }