How To Create A Unit Test File In Magento 2

Mageplaza publishes Magento 2 Unit Test article, that belongs to the free knowledge base string for Magento 2 store owners. The topic is helpful for those who are 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.

Main contents

How to Create a Unit Test file

What is Unit Testing

Unit Testing is a crucial operation, a set of the smallest testable parts of an application, which are called units, in the development of 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 is necessary

Magento 2 Unit Test offers numerous benefits which would definitely be worth your effort in creating and running it. However, below I will list three advantages of using a unit test that I think are most noticeable.

First of all, it allows auto-defining some errors before launching the software instead of that the developers have to do it by themselves. These errors including both bugs in the implementation of the programmer and the specification’s flaws or missing parts for the unit. As a result, writing a thorough set of testing process would push authors to think through inputs, outputs as well as the error conditions, which would help define the desired behavior of the unit crisply.

Secondly, it enables you to facilitates changes easily. More specifically, the programmer is allowed to refactor code or upgrade system libraries at a later date, which would ensure the module works perfectly. This purpose of this procedure is to write test cases for all functions and methods, which would help quickly identify the fault when it happens.

Last but not least, writing the unit test can keep you focused and help you create better designs as before writing it, you have to think through your design and the thing that must be accomplished. Besides, when you test a piece of code, you will have to define the thing that this code is responsible for, which would lead to well-defined code’s responsibility as well as high cohesion.

Create Unit Test file

You can assume that you have a custom module called Testing under the Mageplaza namespace (app/code/Mageplaza/Testing). Then your unit tests will reside inside the Test/Unit folder based on the standards of naming.

The Class that is going to be tested would be called SampleClass and it will reside in the TestingClass folder. Therefore, the whole path will be app/code/Mageplaza/Testing/TestingClass/SampleClass

Below are how the class code looks like:

namespace Mageplaza\Testing\TestingClass;
class SampleClass
    public function getMessage()
        return 'Hello, this is sample test';

You might notice that this class is quite straightforward and it just returns a simple string which is Hello, this is sample test.

When you test getMessage() method the test will look like the following:

namespace Mageplaza\Testing\Test\Unit;
use Mageplaza\Testing\TestingClass\SampleClass;
class SampleTest extends \PHPUnit\Framework\TestCase
     * @var Mageplaza\Testing\TestingClass\SampleClass
    protected $sampleClass;
     * @var string
    protected $expectedMessage;
    public function setUp()
        $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
        $this->sampleClass = $objectManager->getObject('Mageplaza\Testing\TestingClass\SampleClass');
        $this->expectedMessage = 'Hello, this is sample test';
    public function testGetMessage()
        $this->assertEquals($this->expectedMessage, $this->sampleClass->getMessage());

You can see that this unit test has tested the getMessage() method output. The test need to be equal to expectedMessage property in the test so it can pass. With these test extends PHPUnit, you would have the ability to gives up assertEquals() method as well as various testing functionalities. Besides, a setUp() method is also available which are run before methods inside of a test is testing. It can help sets up the testing environment in company with all the class or property that you will need in the test.


The unit test plays an essential role in improving your manageability. By allowing auto-defining some errors before launching the software instead of that the developers have to do it by themselves, a great amount of time and effort of admins can be saved as well as considerably eliminating the errors when launching your software.

Don’t forget to share this article with Magento friends to help them out.

Thanks for reading!

Enjoyed the tutorial? Spread it to your friends!