Back to Top

Unit Test Implementation in Magento 2

Updated 29 August 2024

As in the previous blog, we have learned a 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 of the Magento Directory structure and know how to create a custom module in Magento 2.

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 a model  Calculator here :
app/code/<vendorname>/<modulename>/Model/Calculator.php

Searching for an experienced
Magento 2 Company ?
Find out More
<?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 that will receive 2 values and return the addition as a result.

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

Step 3:  Create a 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(): 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 the test for the particular file.

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

This will return the below output.
On Success :

screenshot27

On Failure: When I changed the logic to subtract.

screenshot29

A 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 you can check PHPUnit.

If you require technical support, feel free to email us at [email protected].

Additionally, explore a wide array of solutions to boost your store’s capabilities by visiting the Adobe Commerce modules section.

For expert advice or to create tailored features, hire Adobe Commerce Developers for your project.

. . .

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