How To Create A Unit Test File In Magento 2

Unit testing is essential to developing high-quality software, and Magento 2 provides a robust testing framework that makes it easy to write and run unit tests. This article will review how to create a unit test file in Magento 2 and provide some sample code and commands to help you get started.


Before we begin, there are a few prerequisites that you need to meet:

  1. You should have Magento 2 installed and set up in your development environment.
  2. PHPUnit should be installed on your system. You can check this by running the command phpunit –version in your terminal.
  3. custom module in your Magento 2.

Steps to create a unit test file in Magento 2

  • Setup test environment in Magento 2
  • Create the test file in Magento 2
  • Run the test file in Magento 2

Setup test environment in Magento 2

First, you need to create a directory to store your test files. In the root directory of your module, create a directory called Test, and inside that directory, create another directory called Unit. This is where you will store your unit test files.

└── code/
    └── GDBlogger/
        └── DemoModule/
            ├── Test/
            │   └── Unit/
            │       └── SampleModelTest.php
            └── …

Testing the Model file

You can assume that you have a custom module called DemoModule under the GDBlogger namespace. And there is a model with the name SampleModel.php inside the Model directory.

Code of SampleModel.php


namespace GDBlogger\DemoModule\Model;

class SampleModel
    public function myMethod()
        return 'Hello, this is a unit test';

Create the test file in Magento 2

Inside the Unit directory, create a new PHP file that will contain your unit tests. The name of the file should reflect the name of the class that you are testing, with the suffix Test. For example, if you are testing a class called SampleModel, the name of your test file should be SampleModelTest.php.



Code of SampleModel.php


namespace GDBlogger\DemoModule\Test\Unit;

class SampleModelTest extends \PHPUnit\Framework\TestCase
    protected $_objectManager;

    protected $_model;

     * This function is called before the test runs.
     * Ideal for setting the values to variables or objects.
    protected function setUp(): void
        $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
        $this->_model = $this->_objectManager->getObject("GDBlogger\DemoModule\Model\SampleModel");

    public function testMyMethod()
        $result = $this->_model->myMethod();
        $expectedResult = 'Hello, this is a unit test';
        $this->assertEquals($expectedResult, $result);

Run the test file in Magento 2

Once you have written your test code, you can run your tests to make sure that they pass. Here’s how to do it:

  • Open a terminal window and navigate to the root directory of your Magento 2 installation.
  • Run the command to run all of the unit tests in your Magento 2 installation. php bin/magento dev:tests:run unit
  • To run a specific test file, use the command
sudo php ./vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/GDBlogger/DemoModule/Test/Unit/SampleModelTest.php

I hope this guide will be helpful to you. Please do not hesitate to contact us through the comments section if you have any further questions.

Leave a comment

Your email address will not be published. Required fields are marked *