Assertions

Overview

By now you've caught a glimpse of some available assertions. They are the ones that actually perform the checks to ensure that things are going as planned.

Remember, the $this variable inside the given closure in tests is always bound to a Test Case class. Therefore assertions are methods of the $this variable.

1it('asserts true is true', function () {
2 $this->assertTrue(true);
3});

Available Assertions

For the full list of assertions, please refer to PHPUnit Assertions documentation.

assertTrue()

The assertTrue asserts the given value is truthy.

1$this->assertTrue(true);

assertFalse()

The assertFalse asserts the given value is falsy.

1$this->assertFalse(false);

assertCount()

The assertCount asserts the given iterable to contain the same number of items.

1$array = [1, 2, 3, 4];
2 
3$this->assertCount(4, $array);

assertEquals()

The assertEquals asserts the given values are equal.

1$array = [1, 2, 3, 4];
2 
3$this->assertEquals([1, 2, 3, 4], $array);

assertEmpty()

The assertEmpty asserts the given iterable is empty.

1$array = [];
2 
3$this->assertEmpty($array);

assertStringContainsString()

The assertStringContainsString asserts the given string exists.

1$this->assertStringContainsString('Star', 'Star Wars');

Next section: Expectations →