Understanding Assertions - PHPUnit part 2

Slide Image
Share:

Introduction to Assertions

Assertions are the foundation of unit testing in PHPUnit. They allow you to compare expected outcomes with actual results to verify the correctness of your code. PHPUnit provides a wide range of assertion methods to validate different conditions.

Commonly Used Assertions in PHPUnit

Here are some frequently used assertion methods in PHPUnit:

  • assertEquals($expected, $actual): Checks if two values are equal.
  • assertSame($expected, $actual): Checks if two values are identical (same type and value).
  • assertNotEquals($expected, $actual): Ensures that two values are not equal.
  • assertTrue($condition): Verifies that a condition is true.
  • assertFalse($condition): Verifies that a condition is false.
  • assertNull($variable): Ensures that a variable is null.
  • assertNotNull($variable): Ensures that a variable is not null.

Testing a Function Using Multiple Assertions

When testing functions, you may need to validate multiple conditions. Let's consider a simple function that performs mathematical operations:

function calculateSum($a, $b) {
    return $a + $b;
}

Writing a Test Case with Multiple Assertions

Create a test file tests/MathTest.php:

<?php
use PHPUnit\Framework\TestCase;

class MathTest extends TestCase
{
    public function testCalculateSum()
    {
        $this->assertEquals(5, calculateSum(2, 3));
        $this->assertEquals(0, calculateSum(0, 0));
        $this->assertEquals(-5, calculateSum(-2, -3));
        $this->assertNotEquals(6, calculateSum(2, 3));
    }
}

Run the test:

vendor/bin/phpunit tests/MathTest.php

Expected output:

OK (1 test, 4 assertions)

Ensuring Incorrect Results Are Not Returned

Using Multiple Test Methods

Instead of writing multiple assertions in a single method, we can split them into separate test methods for better clarity and debugging.

<?php
use PHPUnit\Framework\TestCase;

class MathTest extends TestCase
{
    public function testSumWithPositiveNumbers()
    {
        $this->assertEquals(7, calculateSum(3, 4));
    }

    public function testSumWithNegativeNumbers()
    {
        $this->assertEquals(-10, calculateSum(-5, -5));
    }

    public function testSumWithZero()
    {
        $this->assertEquals(0, calculateSum(0, 0));
    }

    public function testSumDoesNotReturnIncorrectResult()
    {
        $this->assertNotEquals(10, calculateSum(4, 3));
    }
}

Each method tests a specific case, making debugging easier. If one test fails, PHPUnit will clearly indicate which one failed and why.

Conclusion

This article explored the basics of PHPUnit assertions, how to use multiple assertions in a single test method, and how to validate incorrect results using separate test methods. In the next part, we will dive deeper into testing exceptions, mocking dependencies, and best practices for writing effective unit tests in PHPUnit.

Loading...

Loading...

Loading...

Top Categories

  • Loading...
  • Loading...
  • Loading...