---
title: "How To Create A Unit Test File In Magento 2?"
url: "https://magecomp.com/blog/create-a-unit-test-file-in-magento-2/"
date: "2021-03-27T12:56:32+00:00"
modified: "2025-01-29T04:48:11+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "Magento Tutorials"
word_count: 617
reading_time: "4 min read"
summary: "Hello Magento Folks,"
description: "Unit testing is performed to diminish the number of bugs/errors occurred during production. Learn How To Create A Unit Test File In Magento 2"
keywords: "Magento Tutorials"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Guide to Implement Autocomplete Address Field in Magento 2"
    url: "https://magecomp.com/blog/autocomplete-address-field-magento-2/"
  - title: "Guide on Generating Coupon Codes in Magento 2?"
    url: "https://magecomp.com/blog/create-coupon-codes-in-magento-2/"
  - title: "How to Add Top Link in Magento 2"
    url: "https://magecomp.com/blog/add-top-link-in-magento-2/"
---

# How To Create A Unit Test File In Magento 2?

_Published: March 27, 2021_  
_Author: Dhiren Vasoya_  

![How-To-Create-A-Unit-Test-File-In-Magento-2](https://magecomp.com/blog/wp-content/uploads/2021/03/How-To-Create-A-Unit-Test-File-In-Magento-2-png.webp)

**Hello Magento Folks,**

In this article, I will illustrate How To Create A Unit Test File In Magento 2 and How to Run Unit Test in Magento 2. I am writing this for all the beginners who just started with Magento and need to learn about the unit tests. Also, check out our previously placed blog where I have explained [**How to Use Local & Cookie Storage using JavaScript modules in Magento 2**](https://magecomp.com/blog/use-local-cookie-storage-using-javascript-modules-in-magento-2/). We will also go through the Command Line Interface (CLI) and PHPStorm IDE in this article.
## What is Unit Testing?
Basically, unit testing is known as a part of software testing where each and every small unit of the software is tested. The main cause of this is for the validation of the software code which works as expected. Unit testing is conducted when the development process of the application.
## Why do you need Unit Testing?
Mainly unit testing is performed to diminish the number of bugs/errors occurred during production. The precise unit testing is carried out from the closest developer to the project and with the help of these unit tests, all other developers will get an opportunity to understand and learn the code more precisely.
## Benefits of Unit Testing:
**Detect Problems Early –** With unit testing, you can find common errors and corrections at the time of development itself.
**Help in Changes –** Upgrading the libraries and refactoring code will be easy in the future with the help of unit testing.
**Design –** Testing code beforehand will help you create better designs.
**Save Time –** Unit testing at regular intervals will reduce errors and this will reduce the time of testing.
In Magento 2, unit testing means validating pieces of code. Unit testing will help to make sure that the code matches the Magento standards.
## Steps to Create A Unit Test File In Magento 2:
**Step 1:** Create the **Unittest.php** file at the given below path and add code.
**app\code\Vendor\Extension\Model\**

```
<?php

namespace VendorExtensionModel;

class Unittest
{
    /**

     * this function will perform the addition of two numbers

     *

     * @param int $no1

     * @param int $no2

     * @return int

     */

    Public function additiondata($no1 ,$no2)
   {
        return $no1 + $no2;
    }
}
```

**Step 2:** Create the **Unittest.php** file at the given below path and add code.
**app\code\Vendor\Extension\Test\Unit\Model\**

```
<?php

namespace VendorExtensionTestUnitModel;

class Unittest extends PHPUnitFrameworkTestCase
{
    public function setUp() : void
    {
        $this->_objectManager = new MagentoFrameworkTestFrameworkUnitHelperObjectManager($this);

        $this->_calculator = $this->_objectManager->getObject("VendorExtensionModelUnittest");
    }

    /**

     * this function will perform the addition of two numbers

     *

     * @param int $no1

     * @param int $no2

     * @return int

     */

    public function testcaseAddition(): void
    {
        $this->_actulResult = $this->_calculator->additiondata(7,7); // call additiondata method  from model  file

        $this->_desiredResult = 14; // actual result compare  set

        $this->assertEquals($this->_desiredResult, $this->_actulResult); /* check test case match or not if true the give ok message otherwise give error */
    }
}
```

## How to Run Unit Test in Magento 2?
**Test all the Unit test in Magento Core modules**

```
php bin/magento dev:tests:run unit
```

 **Unit test for a specific module**

```
php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Vendor/Extension
```

**Unit Test for a specific file**

```
php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Vendor/Extension/Test/Unit/Model/Unittest.php
```

## Wrap Up:
Hopefully, all are able to Create A Unit Test File and Run Unit Test In Magento 2 using the above-given illustration. In case of any errors, you face during the unit file creation you can ask me in the comment section below. Don’t forget to hit the 5 stars
Share the article with your new developer friends
**Happy Reading**


---

_View the original post at: [https://magecomp.com/blog/create-a-unit-test-file-in-magento-2/](https://magecomp.com/blog/create-a-unit-test-file-in-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-21 22:27:49 UTC_  
