1.7 (34.47%) 47 votes

In this topic we will show you How to Write Unit Test in Magento 2 (Part 1) through 5 steps:

Step 1: Create module Magestore_HelloMagento

Step 2: Create Model HelloMessage.php

Step 3: Create UnitTest Model

Step 4: Add the UnitTest to the Test Suite

Step 5: Run UnitTest

Magento 2 has pre-installed PHPUnit, an automated testing framework for PHP. So we can easy write Unit Test for Magento 2. I will use Magestore_HelloMagento module in my example of this topic today.

Step 1: Create module Magestore_HelloMagento

Please follow steps in the previous post How to create a module in Magento 2

Step 2: Create Model HelloMessage.php

\app\code\Magestore\HelloMagento\Model\HelloMessage.php

The I will write UnitTest for this model.

<?php namespace Magestore\HelloMagento\Model; class HelloMessage { public function getMessage() { return 'Hello Magento 2! We will change the world!'; } } 1 2 3 4 5 6 7 8 9 < ? php namespace Magestore \ HelloMagento \ Model ; class HelloMessage { public function getMessage ( ) { return 'Hello Magento 2! We will change the world!' ; } }

Step 3: Create UnitTest Model

\app\code\Magestore\HelloMagento\Test\Unit\Model\HelloMessageTest.php

<?php namespace Magestore\HelloMagento\Test\Unit\Model; use Magestore\HelloMagento\Model\HelloMessage; class HelloMessageTest extends \PHPUnit_Framework_TestCase { /** * @var HelloMessage */ protected $helloMessage; public function setUp() { $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->helloMessage = $objectManager->getObject('Magestore\HelloMagento\Model\HelloMessage'); $this->expectedMessage = 'Hello Magento 2! We will change the world!'; } public function testGetMessage() { $this->assertEquals($this->expectedMessage, $this->helloMessage->getMessage()); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 < ? php namespace Magestore \ HelloMagento \ Test \ Unit \ Model ; use Magestore \ HelloMagento \ Model \ HelloMessage ; class HelloMessageTest extends \ PHPUnit_Framework_TestCase { /** * @var HelloMessage */ protected $ helloMessage ; public function setUp ( ) { $ objectManager = new \ Magento \ Framework \ TestFramework \ Unit \ Helper \ ObjectManager ( $ this ) ; $ this -> helloMessage = $ objectManager -> getObject ( 'Magestore\HelloMagento\Model\HelloMessage' ) ; $ this -> expectedMessage = 'Hello Magento 2! We will change the world!' ; } public function testGetMessage ( ) { $ this -> assertEquals ( $ this -> expectedMessage , $ this -> helloMessage -> getMessage ( ) ) ; } }

setUp(): Prepare the HelloMessage Model which I need to test, prepare the expected result ($this->expedtedMessage) In this UnitTest Model, I has declared two methods below:

testGetMessage(): compare the result of getMessage() method in HelloMessage model with the expected message which I defined in setUp() method.

Step 4: Add the UnitTest to the Test Suite

\dev\tests\unit\phpunit.xml.dist

<span style="font-weight: 400;"> </span> 1 < span style = "font-weight: 400;" > < / span >

<testsuite name="Magento Unit Tests"> <directory suffix="Test.php">../../../app/code/Magestore/HelloMagento/Test/Unit</directory> <!-- <directory suffix="Test.php">../../../app/code/*/*/Test/Unit</directory> <directory suffix="Test.php">../../../dev/tools/*/*/Test/Unit</directory> <directory suffix="Test.php">../../../dev/tools/*/*/*/Test/Unit</directory> <directory suffix="Test.php">../../../lib/internal/*/*/Test/Unit</directory> <directory suffix="Test.php">../../../lib/internal/*/*/*/Test/Unit</directory> <directory suffix="Test.php">../../../setup/src/*/*/Test/Unit</directory> <directory suffix="Test.php">../../../vendor/*/module-*/Test/Unit</directory> <directory suffix="Test.php">../../../vendor/*/framework/Test/Unit</directory> <directory suffix="Test.php">../../../vendor/*/framework/*/Test/Unit</directory> --> </testsuite> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 < testsuite name = "Magento Unit Tests" > < directory suffix = "Test.php" > . . / . . / . . / app / code / Magestore / HelloMagento / Test / Unit < / directory > < ! -- < directory suffix = "Test.php" > . . / . . / . . / app / code /*/*/ Test / Unit < / directory > < directory suffix = "Test.php" > . . / . . / . . / dev / tools /*/*/ Test / Unit < / directory > < directory suffix = "Test.php" > . . / . . / . . / dev / tools /*/*/ * / Test / Unit < / directory > < directory suffix = "Test.php" > . . / . . / . . / lib / internal /*/*/ Test / Unit < / directory > < directory suffix = "Test.php" > . . / . . / . . / lib / internal /*/*/ * / Test / Unit < / directory > < directory suffix = "Test.php" > . . / . . / . . / setup / src /*/*/ Test / Unit < / directory > < directory suffix = "Test.php" > . . / . . / . . / vendor /*/module-*/ Test / Unit < / directory > < directory suffix = "Test.php" > . . / . . / . . / vendor /*/framework/Test/Unit</directory> <directory suffix="Test.php">../../../vendor/*/ framework / * / Test / Unit < / directory > -- > < / testsuite >

Step 5: Run UnitTest

Open the Console, then run command below from my Magento folder

php ../../../vendor/phpunit/phpunit/phpunit 1 php . . / . . / . . / vendor / phpunit / phpunit / phpunit

I got the messages:

Time: 53 ms, Memory: 4.50MB

OK (1 test, 1 assertion)

Now I change the message in HelloMessage Mode

<span style="font-weight: 400;"> </span> 1 < span style = "font-weight: 400;" > < / span >

public function getMessage() { return 'Hello Magento 1! We will change the world!'; } 1 2 3 4 public function getMessage ( ) { return 'Hello Magento 1! We will change the world!' ; }

Then I run the UnitTest again. I got the message below:

Time: 33 ms, Memory: 4.75MB There was 1 failure: 1) Magestore\HelloMagento\Test\Unit\Model\HelloMessageTest::testGetMessage Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'Hello Magento 2! We will change the world!' +'Hello Magento 1! We will change the world!' /opt/lampp/htdocs/magento2/2.1/01/app/code/Magestore/HelloMagento/Test/Unit/Model/HelloMessageTest.php:22 FAILURES! Tests: 1, Assertions: 1, Failures: 1. 1 2 3 4 5 6 7 8 9 10 11 12 13 Time : 33 ms , Memory : 4.75MB There was 1 failure : 1 ) Magestore \ HelloMagento \ Test \ Unit \ Model \ HelloMessageTest :: testGetMessage Failed asserting that two strings are equal . -- - Expected ++ + Actual @ @ @ @ - 'Hello Magento 2! We will change the world!' + 'Hello Magento 1! We will change the world!' / opt / lampp / htdocs / magento2 / 2.1 / 01 / app / code / Magestore / HelloMagento / Test / Unit / Model / HelloMessageTest . php : 22 FAILURES ! Tests : 1 , Assertions : 1 , Failures : 1.

Thank you for reading this post and see you in other posts from Magestore!The 5 steps I mention above is the shortest process for you to Write Unit Test in Magento 2. With this guide, you can manage the Unit Test in Magento 2 easily. Every store has a Unit Test in Magento 2 with many attributes.