Merry Christmas, 25% OFF. Coupon: XMAS Explore

Magento 2 Unit Test

magento-2-tutorial
unit
test

Mageplaza publishes Mageplaza 2 Unit Test article, that belongs the free knowledge base string for Magento 2 store owners. The topic is helpful for those who are the beginners and really want to get familiar with the unit tests. To make the guide clear, we will approach to two different ways including:

  • Command Line Interface (CLI)
  • PHPStorm IDE

Before carrying out the unit tests in Magento 2, let’s glance at the overview of the unit test.

What is Unit Test?

Unit Testing is a crucial operation, a set of the smallest testable parts of an application, which are called as units, in the development of a software. The unit testing will ensure that the codes you wrote are running correctly as well as raise the software’s quality you created before. In addition, remember that the unit testing process is separate and completely automatic without any manual handling.

Why Unit Test?

Unit Test allows auto-defining some errors before launching the software instead of that the developers have to do it by themselves. Due to that, the more powerful unit testing process, the better management of the project managers and functional managers. The unit test plays a role in improving the manageability like:

  • Tests Reduce Bugs in New Features
  • Tests Reduce Bugs in Existing Features
  • Tests Are Good Documentation
  • Tests Reduce the Cost of Change
  • Tests Allow Refactoring
  • Tests Constrain Features
  • Tests Defend Against Other Programmers
  • Testing Makes Development Faster

Thus, generating a unit test is the important first step for the high quality of the new class. Don’t forget that you can find the unit test in app/code/[Vendor]/[ModuleName]/Test/Unit.

How to run Magento 2 Unit Test

Step 1: Create a test file

Create a test file: app/code/Mageplaza/Test/Unit/Model/PostTest.php

<?php 


namespace Mageplaza\HelloWorld\Test\Unit\Model;
 
 
class PostTest extends \PHPUnit_Framework_TestCase {

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

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

    /**
     * Is called before running a test
     */
    protected function setUp()
    {
        
    }

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

    /**
     * The test itself, every test function must start with 'test'
     */
    public function testTest()
    {
        $this->assertTrue(false);
    }
}

Step 2: Run a test

Run the following command to check the result

bin/magento dev:tests:run unit


It comes to the end of tutorial: Magento 2 Unit Test.

Please leave comments if you have any questions, feedbacks.

Comments for Magento 2 Unit Test

You also may like these Magento 2 Extensions

One Step Checkout

$199
18 reviews

Layered Navigation

$149
7 reviews

Store Locator

$199
no review

Shop By Brand

$99
no review

Zoho CRM

$249
no review

FAQ

$149
10 reviews

Better Blog

FREE
7 reviews

Affiliate

$149
7 reviews

People also searched for:

  • Magento 2 Unit Test