Reading list Switch to dark mode

    Assertions in Unit Test Magento 2 Open Source and Adobe Commerce

    Updated 5 August 2022

    The assertion is a boolean expression at a specific point in the program which will be true and it will match the expected and actual value in Unit Testing when the expected and actual values will be the same the test will pass and fail when the result is not the same.

    There are different types of assertions in Unit Testing. We have described some of the assertions techniques below.

    assertEquals() Function :

    The assertEquals() is a built-in function in PHPUnit and it is assert whether the actual obtained value is equal to the expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false. when the asserted value is the true test case will pass or else fail.

    <?php
    namespace namespace Webkul\UnitTest\Test\Unit\Helper;
    
    class DataTest extends \PHPUnit\Framework\TestCase
    {
        public function testcaseForAssertEquals()
        {
            $expected = "Webkul";
            $actual = "Webkul";
      
            // Assert function to test whether expected
            // value is equal to actual or not
            $this->assertEquals(
                $expected,
                $actual,
                "actual value is equals to expected"
            );
        }
    }

    assertSame() Function :

    The assertSame() function is a built-in function in PHPUnit and it is assert whether the actually obtained value is the same as the expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false. This string message comes as an error when the test will failed.

    <?php
    namespace namespace Webkul\UnitTest\Test\Unit\Helper;
    
    class DataTest extends \PHPUnit\Framework\TestCase
    {
        public function testcaseForAssertSame()
        {
            $expected = "Webkul";
            $actual = "Webkul";
      
            // Assert function to test whether expected
            // value is equal to actual or not
            $this->assertSame(
                $expected,
                $actual,
                "actual value is equals to expected"
            );
        }
    }

    Parameters: This function accepts three parameters as shown in the above syntax. Description of parameters are below :

    Searching for an experienced
    Magento 2 Company ?
    Find out More
    • $expected: This parameter is of any type which represents the expected data.
    • $actual: This parameter is of any type which represents the actual data.
    • $message: This parameter takes a string value. This string message comes as an error when the test will failed.

    assertContains() Function :

    The assertContains() function is a built-in function in PHPUnit and it is assert an array having a value. This assertion will return true in the case if the array contains the provided value else return false and in the case of true the asserted test case got passed else test case got failed.

    <?php
    namespace namespace Webkul\UnitTest\Test\Unit\Helper;
    
    class DataTest extends \PHPUnit\Framework\TestCase
    {
        public function testcaseForAssertContains()
        {
            $testArray = array("a"=>"value a", "b" =>"value b");
            $value = "value b"; 
            // assert function to test whether 'value' is a value of array
            $this->assertContains(
                $value,
                $testArray, 
                "testArray contains value as value"
            ) ;
        }
    }

    Parameters: This function accepts three parameters as shown in the above syntax. Description of parameters are below :

    • $value: This parameter represents the value contained in the array.
    • $testArray: This parameter is an array for which the assert function will check whether it contains a value or not.
    • $message: This parameter takes a string value. This string message comes as an error when the test will failed.
    <?php
    namespace namespace Webkul\UnitTest\Test\Unit\Helper;
    
    class DataTest extends \PHPUnit\Framework\TestCase
    {
        public function testcaseForAssertContains()
        {
            $testArray = array(1, 2, 3);
      
            // Assert function to test whether testArray contains
            // same number of elements as expectedCount
            $expectedCount = 3;
      
            $this->assertCount(
                $expectedCount,
                $testArray,
                "testArray contains 3 elements"
            );
        }
    }

    Parameters: This function accepts three parameters as shown in the above syntax. Description of parameters are below :

    • $expectedCount: The parameter is the expected count of value in an array.
    • $testArray: This parameter is the array for which the assert function will check whether it contains the same number of elements as the given count value or not.
    • $message: This parameter takes a string value. This string message comes as an error when the test will failed.

    assertArrayHasKey() Function :

    The assertArrayHasKey() function is a built-in function in PHPUnit and is used to assert whether an array has a particular key or not. This assertion will return true in the case if the array has the provided key else return false and in the case of true the asserted test case got passed else test case got failed.

    <?php
    namespace namespace Webkul\UnitTest\Test\Unit\Helper;
    
    class DataTest extends \PHPUnit\Framework\TestCase
    {
        public function testcaseForArrayHasKey()
        {
            // array to be tested
            $array  = array('test' => 'webkul');
    
            // assert function to test whether 'test' is a key of array
            $this->assertArrayHasKey(
                'test',
                $array,
                "Array contains 'test' as key"
            );
        }
    }

    Parameters: This function accepts three parameters as shown in the above syntax. Description of parameters are below :

    • test: This parameter is the expected key of the array.
    • $array: This parameter is the array for which the assert function will check whether it contains the same key of elements as the given key value or not.
    • $message: This parameter takes a string value. This string message comes as an error when the test will failed.

    assertArraySubset() Function :

    The assertArraySubset() function is a built-in function in PHPUnit and it is assert an array having a subset. his assertion will return true in the case if the array contains the provided subset or else return false. In the case of true the asserted test case got passed else test case got failed.

    <?php
    namespace namespace Webkul\UnitTest\Test\Unit\Helper;
    
    class DataTest extends \PHPUnit\Framework\TestCase
    {
        public function testcaseForArrayHasSubset()
        {
            $array  = array('test' => 'webkul', 'test1' => 'noida');
            $subset = array('test' => 'webkul');
    
            // assert function to test whether 'subset' is a subset of array
            $this->assertArraySubset(
                $subset,
                $array,
                $flag = false, 
                "array contains subset as subset"
            ) ;
    
        }
    }

    Parameters: This function accepts three parameters as shown in the above syntax. Description of parameters are below :

    • $subset: This parameter represent the subset to be present in an array.
    • $array: This parameter is an array for which the assert function will check whether the provided subset is available or not.
    • $flag: This parameter is a flag and it will compare the identity of objects within arrays.
    • $message: This parameter takes a string value. This string message comes as an error when the test will failed.

    assertTrue() Function :

    The assertTrue() is a PHPUnit built-in function. It is reporting an error according to the condition.

    <?php
    namespace namespace Webkul\UnitTest\Test\Unit\Helper;
    
    class DataTest extends \PHPUnit\Framework\TestCase
    {
        public function testcaseForAssertTrue()
        {
            // assert function
            $this->assertTrue(true) ;
    
        }
    }

    assertFalse() Function :

    The assertFlase() is a PHPUnit built-in function. It is reporting an error according to the condition.

    <?php
    namespace namespace Webkul\UnitTest\Test\Unit\Helper;
    
    class DataTest extends \PHPUnit\Framework\TestCase
    {
        public function testcaseForAssertFalse()
        {
            // assert function
            $this->assertFalse(false);
    
        }
    }

    assertFileEquals() Function :

    The assertFileEquals() reports an error identified by message if the file specified by expected does not have the same contents as the file specified by actual.

    <?php
    namespace namespace Webkul\UnitTest\Test\Unit\Helper;
    
    class DataTest extends \PHPUnit\Framework\TestCase
    {
        public function testcaseForAssertFileEquals()
        {
            // assert function for two files are equal or not
            $this->assertFileEquals('/home/sb/expected', '/home/sb/actual');
    
        }
    }

    assertFileExists() Function :

    The assertFileExists() reports an error identified by message if the file specified by filename does not exist.

    <?php
    namespace namespace Webkul\UnitTest\Test\Unit\Helper;
    
    class DataTest extends \PHPUnit\Framework\TestCase
    {
        public function testcaseForAssertFileExists()
        {
            // assert function for two files are equal or not
            $this->assertFileExists('path to file');
    
        }
    }

    These are some important assertions that we used in Magento for Unit Testing.

    Thank’s for reading this. If you have any queries please comment below.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home