Menu Close
    Searching for an experienced Magento 2 Development Company ?

    Unit Test Implementation in Magento 2 custom module

    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.

    Let me brief I am going to create a model for calculator where we can perform basic operations like addition, substraction etc.
    Step 1 : Create your custom module and register it, in may case I have create module Webkul_HelloUnitTest.

    Step 2 : Create model  calculator here :
    app/code/<vendorname>/<modulename>/Model/Calculator.php

    <?php
    
    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 recieve 2 values and return the addition as result.

    Now lets create unit test model for the above.

    Step 3 :  Create test model here :
    app/code/<vendorname>/<modulename>/Test/Unit/Model/Calculator.php

    <?php
    
    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() {
            
        }
    
        /**
         * used to set the values to variables or objects.
         *
         * @return void
         */
        public function setUp() {
            $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.

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    Be the first to comment.

    Back to Top