Magento 2 comes with an automated testing framework named “PHPUnit”. It is pre-installed and included as one of the dependencies in Magento 2. The objective of Unit Testing is to isolate and verify a section of code for it correctness. In procedural programming, a unit may be a single function or procedure. Unit Testing is usually performed by the developer who isolates each part of the program and checks its correctness.

Why unit testing is important?

Sometimes software developers try to save time by skipping few unit testing procedures which leads to more issues during System Testing, Integration Testing and even in Beta testing of that particular application. Proper unit testing during the development process saves time and money at the end.

Unit Testing Benefits

Unit testing increases confidence in changing/maintaining module code. If we create good unit test procedures and run every time when code changed, we will be able to promptly catch any defects due to the recent code changes. Also, if codes are already made less interdependent to make unit testing possible, the unintended impact of changes to any code is less. In order to make unit testing possible, codes need to be modular. That means,codes are easier to reuse.

Comparing with the cost and effort of fixing defects in the development stage is lesser than the cost (time, effort, destruction, indignity) of a defect detected during higher level testing or when the software is live.

When a unit test fails, only the latest changes need to be debugged. But, in higher level testing, the changes made over a large span of time like days/weeks/months need to be debug.

In this blog we will show you how to write Unit Test in Magento 2:
• Step 1: Create a module named Titech_HelloWorld
• Step 2: Create block HelloWorld.php
• Step 3: Create UnitTest block
• Step 4: Add the UnitTest to the TestSuite
• Step 5: Run UnitTest

Step 1: Create a module Titech_HelloWorld

Note: Click here to read instruction about how to create a module in Magento 2

Step 2: Create Block HelloWorld.php

File location: \app\code\Titech\HelloWorld\Block\HelloMessage.php

 

 

 

 

 

 

 

Step 3: Create UnitTest Block

File location: \app\code\Titech\HelloWorld\Test\Unit\Block\HelloMessage.php

 

 

 

 

 

 

 

 

• setUp(): Here we are preparing the block that need to test with its expected result in $this->expectedMessage.
• testGetMessage(): Here we are comparing the result of “getHelloWorldTxt()” method with the expected result that defined in “setUp()”.

Step 4: Add the UnitTest to the TestSuite

File location: \dev\tests\unit\phpunit.xml
We need to tell PHPUnit where to find test cases and other related information that need to run during the testing process. Configuration file provide all these information. To make it active, clone the file ‘phpunit.xml.dist’ into same folder and remove the ‘.dist’ extension. Now the file name will be ‘phpunit.xml’. In this file, we need to specify our test location.

In our example, other tests locations are commented out because we don’t want PHPUnit to run all the tests, just the one we have written earlier. So our phpunit.xml should look like this:

 

 

 

 

 

 

Step 5: Run UnitTest

Now, we are going to learn how to run the test. Open the console/terminal, then go to Magento project folder and run below command.

php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml

Test should pass and our console looks like this:

 

 

 

 

Conclusion:
In this article, we have studied Magneto 2 unit testing basics. Its is not possible to cover such a complex topic within a single article But, I tried to encourage you to study more about PHPUnit testing and read more articles. You can find the best testing examples in Magento core itself because, you know, its coming with Magento2!