Create a Unit Test

What is Unit Testing? Right

Magento 2 comes pre-installed with PHPUnit, an automated testing framework for PHP. You can access it only if the Magento_Developer module is enabled. 

Unit testing is the next level of software testing mainly performed by developers where individual components of the software get tested. 

The unit testing will ensure that the codes you wrote are running correctly as well as help you to increase the software’s quality too. Besides, please remember that the unit testing process is separate and completely automatic without any manual handling.

We can use this as a forecast existing issue or problem in the development phase. So having ideas of those problems helps in reducing the cost/time of bug fixes also helps us to build a solution that will be more specific to what we want as desire output.

So let get started with Create a Unit Test ( simple one ) and I promise that I will dig more in-depth in this topic

For creating our extension we need:

  • We need to register our module

  • Creating a model and a Unit Test file

  • Deploy with command

Path : app/code/Nilesh/UnitTest/registration.php
<?php
/**
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.
 */
use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Nilesh_UnitTest', __DIR__);

Path : app/code/Nilesh/UnitTest/etc/module.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Nilesh_UnitTest" setup_version="1.0.0"/>
</config>

Path : app/code/Nilesh/UnitTest/Model/UnitTest.php
<?php

/**
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Nilesh\UnitTest\Model;

class UnitTest
{
    public function addition( int $a, int $b)
    {
        return $a + $b;
    }

    public function message()
    {
        return "Your message";
    }
    
    public function getArray()
    {
        return array("hello" => "world");
    }
}

Path : app/code/Nilesh/HelloWorld/Test/Unit/Model/UnitTestTest.php
<?php
/**
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Nilesh\UnitTest\Test\Unit\Model;

class UnitTestTest extends \PHPUnit\Framework\TestCase
{

    protected $_objectManager;
    protected $modelUnitTest;

    /**
     * Is called once before running all test in class
     */
    public static function setUpBeforeClass()
    {
        //setup
    }

    /**
     * Is called once after running all test in class
     */
    public static function tearDownAfterClass()
    {
        //teardown
    }

    /**
     * Is called before running a test
     */
    protected function setUp()
    {
        //setup
        $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
        $this->modelUnitTest = $this->_objectManager->getObject("Nilesh\UnitTest\Model\UnitTest");
    }

    /**
     * Is called after running a test
     */
    protected function tearDown()
    {
        //teardown
    }

    /**
     * The test itself, every test function must start with 'test'
     */
    public function testAddition()
    {
        $result = $this->modelUnitTest->addition(2,3);
        $expectedResult = 5;
        $this->assertEquals($expectedResult, $result);
        // $this->assertTrue(false);
    }

    /**
     * The test itself, every test function must start with 'test'
     */
    public function testMessage()
    {
        $result = $this->modelUnitTest->message();
        $expectedResult = "Your message";
        $this->assertEquals($expectedResult, $result);
    }
    
    /**
     * The test itself, every test function must start with 'test'
     */
    public function testGetArray()
    {
        $result = $this->modelUnitTest->getArray();
        $testResult = (string) gettype($result);
        $expectedResult = "array";
        $this->assertEquals($expectedResult, $testResult);
    }
}

Command : php bin/magento setup:upgrade
Command: php bin/magento setup:static-content:deploy -f
Command : php bin/magento c:f
Command : php bin/magento dev:tests:run unit
Command for specific file : php -f ./vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist ./app/code/Nilesh/UnitTest/Test/Unit/Model/UnitTestTest.php

Happy coding and Thanks

If you face any issue please follow this link.

For more, you can visit here

You all are the semicolon to my statements; Please support me; Thanks