PHP › Unit Testing with PHPUnit
What are assertions in PHPUnit, and how do commonly used assertions like assertEquals and assertTrue let you verify expected behavior within a test?
BeginnerAnswer
An assertion is a statement within a test that checks whether an actual value matches an expected value, and if that check fails, the entire test is immediately marked as failed with a clear message describing exactly what was expected versus what was actually received, and PHPUnit provides a large number of built in assertion methods covering many common comparison needs, including assertEquals to check that two values are equal, assertTrue and assertFalse to check a boolean condition, and assertCount to check the exact number of items within an array.
public function testUserIsActiveByDefault(): void
{
$user = new User('Sarah');
$this->assertTrue($user->isActive());
}Real-world example A test verifying that a newly created User object is active by default uses the assertTrue assertion to directly check the boolean result returned by calling the isActive method on that new object.