0Pricing
PHP Academy · Lesson

Assertions and Test Methods

Use assertEquals, assertSame, assertNull, and other assertions.

Assertions and Test Methods is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What are Assertions?

Assertions verify that the actual output of code matches the expected value. PHPUnit provides a rich library of assertion methods.

assertEquals vs assertSame

assertEquals uses loose comparison (==). assertSame uses strict comparison (===) — checks type AND value.

<?php
$this->assertEquals(0, false);      // passes (0 == false)
$this->assertSame(0, false);        // FAILS  (0 !== false)

assertTrue and assertFalse

Test that a value is exactly true or exactly false.

<?php
$this->assertTrue(is_string("hello"));
$this->assertFalse(is_numeric("abc"));

assertNull and assertNotNull

Check whether a value is null.

<?php
$user = $repo->find(999);
$this->assertNull($user);

$user = $repo->find(1);
$this->assertNotNull($user);

assertCount

Assert that a countable has the expected number of elements.

<?php
$items = $cart->getItems();
$this->assertCount(3, $items);

assertStringContainsString

Assert that a string contains a substring (case-sensitive).

<?php
$html = $template->render();
$this->assertStringContainsString("<h1>Welcome</h1>", $html);

assertInstanceOf

Assert that an object is an instance of a given class or interface.

<?php
$handler = $factory->create("email");
$this->assertInstanceOf(EmailHandler::class, $handler);

assertThrows / expectException

Expect an exception to be thrown during the test.

<?php
public function test_throws_on_invalid_input(): void
{
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage("must be positive");
    new Product(-1);
}

assertArrayHasKey

Check that an array contains a specific key.

<?php
$result = $api->getUser(1);
$this->assertArrayHasKey("email", $result);
$this->assertArrayHasKey("id", $result);

assertEqualsWithDelta

Compare floating-point numbers within a tolerance (delta).

<?php
$this->assertEqualsWithDelta(3.14159, M_PI, 0.001);

Custom Failure Messages

Pass a message as the last argument to any assertion to improve test failure output.

<?php
$this->assertEquals(200, $response->status(), "Expected HTTP 200 OK response");

Summary

Use assertSame for strict equality, assertEquals for loose, and specialised assertions like assertInstanceOf, assertCount, assertThrows for expressive tests.

Quick Check

Which assertion also checks the type of the value?

Frequently asked questions

Is the “Assertions and Test Methods” lesson free?

Yes — the full text of “Assertions and Test Methods” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Assertions and Test Methods”?

Use assertEquals, assertSame, assertNull, and other assertions. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Assertions and Test Methods” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. PHPUnit Setup and First Test
  2. Assertions and Test Methods
  3. Data Providers for Parameterized Tests
  4. Mocking Dependencies with PHPUnit
← Back to PHP Academy