Reading list Switch to dark mode

    Unit Test Implementation in Magento 2 custom module

    Updated 4 January 2023

    As in my previous blog we have learned few commands to perform unit testing . Today we will create a small custom module in magento 2 and will write test cases for it.
    Let’s start, now I am assuming that you are aware with the Magento Directory structure and know how to create custom module.
    If you are new to Magento 2, so first please check our wonderful blog here , before moving ahead.

    In this blog we are going to create a model for calculator where we can perform basic addition operation.
    Step 1 : Create your custom module and register it, in my case I have created a module Webkul_HelloUnitTest.

    Step 2 : Create model  Calculator here :

    namespace Webkul\HelloUnitTest\Model;
    class Calculator {
         * This function will perform the addition of two numbers
         * @param float $a
         * @param float $b
         * @return float
        Public function addition($a ,$b) {
            return $a + $b;

    Above We have a class calculator and inside it there is a function addition which will receive 2 values and return the addition as result.

    Now let’s create unit test model for the above.

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    Step 3 :  Create test model here :

    namespace Webkul\HelloUnitTest\Test\Unit\Model;
    class Calculator extends  \PHPUnit\Framework\TestCase {
        protected $_objectManager;
        protected $_desiredResult;
        protected $_actulResult;
        protected $_calculator;
         * Unset the variables and objects after use
         * @return void
        public function tearDown(): void {
         * Used to set the values to variables or objects.
         * @return void
        public function setUp(): void {
            $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
            $this->_calculator = $this->_objectManager->getObject("Webkul\HelloUnitTest\Model\Calculator");
            //can do stuff
         * This function will perform the addition of two numbers
         * @param float $a
         * @param float $b
         * @return float
        public function testAddition() { 
             $this->_actulResult = $this->_calculator->addition(7.0,3.0);
             $this->_desiredResult = 10.0;
             $this->assertEquals($this->_desiredResult, $this->_actulResult);

    Step 4 : Run any of the commands of my previous blog.
    Here I have a command to run test for the particular file.
    here it is :

    php vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Webkul/HelloUnitTest/Test/Unit/Model/Calculator.php

    This will return below output.
    On Success :

    On Failure : when I changed the logic to substract.

    Few types of Assertions are as below  :

    assertTrue($x)	Fail if $x is false
    assertFalse($x)	Fail if $x is true
    assertNull($x)	Fail if $x is set
    assertNotNull($x)	Fail if $x not set
    assertIsA($x, $t)	Fail if $x is not the class or type $t
    assertNotA($x, $t)	Fail if $x is of the class or type $t
    assertEqual($x, $y)	Fail if $x == $y is false
    assertNotEqual($x, $y)	Fail if $x == $y is true
    assertWithinMargin($x, $y, $m)	Fail if abs($x - $y) < $m is false
    assertOutsideMargin($x, $y, $m)	Fail if abs($x - $y) < $m is true
    assertIdentical($x, $y)	Fail if $x == $y is false or a type mismatch
    assertNotIdentical($x, $y)	Fail if $x == $y is true and types match
    assertReference($x, $y)	Fail unless $x and $y are the same variable
    assertClone($x, $y)	Fail unless $x and $y are identical copies
    assertPattern($p, $x)	Fail unless the regex $p matches $x
    assertNoPattern($p, $x)	Fail if the regex $p matches $x
    expectError($x)	Fail if matching error does not occour
    expectException($x)	Fail if matching exception is not thrown
    ignoreException($x)	Swallows any upcoming matching exception
    assert($e)	Fail on failed expectation object

    Note : If you want to go deep in the topic so you can check this link .
    Please try out once and If stuck any where so please feel free to ask in comments section.

    . . .

    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