---
title: Magento 2- Integration tests with PHP unit
description: Magento 2 provides different types of PHPUnit tests. Unit tests, Functional tests, Integration tests. We are focusing on  Integration tests in this blog. The purpose of integration test is to ensure that the functionality (Modules) is working as expected with other modules as well. The integration tests work with a database.
image: https://blog.e-zest.com/hubfs/magento-2-integration-tests-with-php-unit.png
---

[![e-Zest logo\_An accion lab company\_Colour](https://blog.e-zest.com/hubfs/images/Logos%20with%20Tagline/Corporate/SVG/e-Zest%20logo_An%20accion%20lab%20company_Colour.svg "e-Zest logo_An accion lab company_Colour")](https://www.e-zest.com/)

- [WORK](https://www.e-zest.com/our-work)
- [SERVICES](https://www.e-zest.com/our-services) 
    - [Product Engineering](https://www.e-zest.com/digital-product-engineering-services)
    - [Enterprise Data Services](https://www.e-zest.com/enterprise-data-services)
    - [Digital Commerce](https://www.e-zest.com/digital-commerce-services)
    - [Digital Operations](https://www.e-zest.com/digital-operations)
    - [Digital Consulting](https://www.e-zest.com/digital-transformation-consulting-services)
    - [Digital Experience Design](https://www.e-zest.com/digital-experience-design)
- [INSIGHTS](https://blog.e-zest.com/)
- [CAREERS](https://www.e-zest.com/careers)
- [ENTERPRISE DATA SERVICES](https://www.e-zest.com/enterprise-data-services)
- [GENERATIVE AI](https://www.e-zest.com/generative-ai)
- [Healthcare](https://healthcare.e-zest.com)
- [Contact Us](https://www.e-zest.com/contact-us)

- ![Elevating Product Engineering with AI and ML](https://blog.e-zest.com/hubfs/Elevating%20Product%20Engineering%20with%20AI%20and%20ML.jpg)

  [BLOGS](https://blog.e-zest.com)
  
  #### Elevating Product Engineering with AI and ML

  As technology evolves and customer choices broaden, there is an increasing ...

  [Read more](https://blog.e-zest.com/elevating-product-engineering-with-ai-and-ml)
- ![e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024](https://blog.e-zest.com/hubfs/e-Zest%20%E2%80%93%20An%20Accion%20Labs%20Company%20-%20Gets%20Recognition%20as%20a%20Contender%20in%20the%20ISG%20Provider%20LensTM%20Study%20on%20Digital%20Engineering%20Services%202024.jpg)

  [NEWS](https://blog.e-zest.com/news)
  
  #### e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024

  We are delighted to announce that e-Zest – An Accion Labs Company has been ...

  [Read more](https://blog.e-zest.com/news/e-zest-an-accion-labs-company-gets-recognition-as-a-contender-in-the-isg-provider-lens-study-on-digital-engineering-services-2024)
- ![e-Zest Solutions supports Digital Pune Mission](https://blog.e-zest.com/hubfs/blog-files/Digital%20Literacy%20Bus%20e-Zest.png)

  [CSR](https://blog.e-zest.com/csr)
  
  #### e-Zest Solutions supports Digital Pune Mission

  **Pune, India:** e-Zest Solutions, a leading digital technology innovation ...

  [Read more](https://blog.e-zest.com/csr/e-zest-solutions-supports-digital-pune-mission)

- [Work](https://www.e-zest.com/our-work)
- [Services](https://www.e-zest.com/our-services)
- [Insights](https://blog.e-zest.com)
- [Careers](https://www.e-zest.com/careers)
- [Enterprise Data](https://www.e-zest.com/enterprise-data-services)
- [Generative AI](https://www.e-zest.com/generative-ai)
- [Innovations](https://www.e-zest.com/innovations)
- [Healthcare](https://healthcare.e-zest.com)
- [About](https://www.e-zest.com/about-us)
- [Contact](https://www.e-zest.com/contact-us)

- [Blog](https://blog.e-zest.com/)
- [News](https://blog.e-zest.com/news)
- [CSR](https://blog.e-zest.com/csr)

- [![Linkedin Icon](https://blog.e-zest.com/hubfs/icons/linkedin_icon_white.svg)](https://www.linkedin.com/company/e-zest-solutions/)
- [![Twitter Icon](https://blog.e-zest.com/hubfs/twitter-logo-silhouette%20(5).svg)](https://twitter.com/ezest)
- [![Facebook Icon](https://blog.e-zest.com/hubfs/facebook-app-logo%20(4).svg)](https://www.facebook.com/61259256638)
- [![Instagram Icon](https://blog.e-zest.com/hubfs/instagram-logo%20(4).svg)](https://www.instagram.com/ezestsolutions/)
- [![Youtube Icon](https://blog.e-zest.com/hubfs/icons/youtube_icon_white.svg)](https://www.youtube.com/user/ezestcorporate)

[![Go to top icon](https://blog.e-zest.com/hubfs/AA%20Assets/top%20button.svg)](https://blog.e-zest.com/magento-2-integration-tests-with-php-unit#top)

Hit enter to search or ESC to close

Insights

e-Zest members share technology ideas to foster digital transformation.

- [Blog](https://blog.e-zest.com)
- [Whitepaper](https://blog.e-zest.com/whitepaper)
- [Playbook](https://blog.e-zest.com/playbook)
- [News](https://blog.e-zest.com/news)
- [Innovation](https://blog.e-zest.com/innovation)
- [Media Kit](https://www.e-zest.com/media-kit)

# Magento 2- Integration tests with PHP unit

[Kailash Lambe](https://blog.e-zest.com/author/kailash-lambe)  Oct 04, 2018

- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/magento-2-integration-tests-with-php-unit&title=&summary=&source=>
- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/magento-2-integration-tests-with-php-unit&text=Magento%202-%20Integration%20tests%20with%20PHP%20unit>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/magento-2-integration-tests-with-php-unit>
- <https://www.instagram.com/ezestsolutions/?u=https://blog.e-zest.com/magento-2-integration-tests-with-php-unit>

Magento 2 provides different types of PHPUnit tests.

- Unit tests
- Functional tests
- Integration tests

In this blog, I am focused on Integration tests.

**Integration tests**

The purpose of integration test is to ensure that the functionality (Modules) is working as expected with other modules as well. The integration tests work with a database.

**Prerequisite**

- A dedicated test database
- The test framework database configuration
- The PHPUnit configuration

 Before starting to create an integration test, let’s get familiar with some terminologies which we are going use in integration tests.

**Fixtures**

Fixtures are a set of values, which can be an object or an array, and it interacts with a test database and confirms that the module/functionality is working as expected.

**Annotations**

In PHP, annotations are meta data that are used to inject some behaviour. We can achieve this by putting the data in comments above the methods.

Magento default provides different types of annotations, which can be used to set fixtures and annotations such as:

- @magentoConfigFixture
- @magentoDbIsolation
- @magentoDataFixture
- @magentoAppIsolation
- @magentoCache
- @magentoComponentsDir
- @magentoAdminConfigFixture
- @magentoAppArea
- @magentoDataFixtureBeforeTransaction

 Let’s create the integration test to add the product into the cart and check the success assertion.

**1) Prepare test database**

As per Magento recommendations, Integration should use a different database than the one in the real Magento instance.

Create new database with all permissions. For every integration test Magento requires a fresh test database.

**2) The test framework database configuration**

Copy the file ./dev/tests/integration/etc/install-config-mysql.php.dist to ./dev/tests/integration/etc/install-config-mysql.php and update the database settings as below.

![database-config](https://blog.e-zest.com/hs-fs/hubfs/database-config.png?width=832&name=database-config.png)

![database-config2](https://blog.e-zest.com/hs-fs/hubfs/database-config2.png?width=1161&name=database-config2.png)  
**3) The PHPUnit configuration**

Copy the file ./dev/tests/integration/phpunit.xml.dist to ./dev/tests/integration/phpunit.xml and update the testsuites settings as per module requirement. If you don’t comment the rest of the testsuites then the Magento core testsuites will run and it will take a hours for completion.

Code in plain text -   
 \<!-- Test suites definition --\>  
 \<testsuites\>  
 \<!-- Memory tests run first to prevent influence of other tests on accuracy of memory measurements --\>  
 \<!-- testsuite name="Memory Usage Tests"\>  
 \<file\>testsuite/Magento/MemoryUsageTest.php\</file\>  
 \</testsuite\>  
 \<testsuite name="Magento Integration Tests"\>  
 \<directory suffix="Test.php"\>testsuite\</directory\>  
 \<exclude\>testsuite/Magento/MemoryUsageTest.php\</exclude\>  
 \</testsuite --\>  
 \<testsuite name="CartTestIntegration"\>  
 \<directory suffix="Test.php"\>../../../app/code/Ezest/\*/Test/Integration\</directory\>  
 \<exclude\>../../../app/code/Magento\</exclude\>  
 \<exclude\>../../../vendor/magento\</exclude\>  
 \</testsuite\>  
 \</testsuites\>

**4) Integration PHP file**  
Create the Magento module and create one main integration file as below at location, ./app/code/Ezest/Integration/Test/Integration/Controller/AddToCartTest.php

![integrate-php](https://blog.e-zest.com/hs-fs/hubfs/integrate-php.png?width=1071&name=integrate-php.png)  
Code in plain text-

\<?php  
namespace Ezest\\Integration\\Test\\Integration\\Controller;  
class AddToCartTest extends \\Magento\\TestFramework\\TestCase\\AbstractController {  
 /\*\*  
 \* @magentoDbIsolation enabled  
 \* @magentoAppIsolation enabled  
 \*  
 \*/  
 public function testAddToCartProducts()  
 {  
 $productId = 1;  
 $objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();  
 $cart = $objectManager-\>create('Magento\\Checkout\\Model\\Cart');  
 $formKey = $objectManager-\>create('Magento\\Framework\\Data\\Form\\FormKey');  
 $product = $objectManager-\>create('Magento\\Catalog\\Model\\Product');  
 $params = array( 'form\_key' =\> $formKey-\>getFormKey(),  
 'product' =\> $productId,  
 'qty' =\>1 //quantity of product  
 );  
 $\_product = $product-\>load($productId);  
 $cart-\>addProduct($\_product, $params);  
 $cart-\>save();  
 $this-\>dispatch('checkout/cart/');  
 $this-\>assertContains('"items\_count":"1"', $this-\>getResponse()-\>getBody());  
 $this-\>assertNotContains('"items\_count":"0"', $this-\>getResponse()-\>getBody());  
 }  
}  
As you can see, we have used two annotations here,

- @magentoDbIsolation enabled

 When this annotation is enabled, each of the tests will be bundle inside a transaction and rolled back on completion.

- @magentoAppIsolation enabled

When this annotation is enabled, the application will be restarted on each test run.

**5) Run the test command as below,**

php bin/magento dev:tests:run integration  
 You will see an output like and the product will be added to the quote table.

![quote-table](https://blog.e-zest.com/hs-fs/hubfs/quote-table.png?width=1053&name=quote-table.png)  
**TESTS\_CLEANUP constant:**

Default value:

**\<const name="TESTS\_CLEANUP" value="disabled"/\>**

if this constant is set to enabled, the integration test framework will clean the test database and reinstall Magento on every test run. This way, any new modules will be automatically picked up, and any cruft that might have been left over from previous test runs will be removed. It also causes the test framework to flush the test Magento configuration.  
If we disable this configuration, the test execution will start much quicker, consequently the developer must manually flush the cache and the database when needed.

To force the test framework to regenerate the cache and the other files, remove the directory  
**rm -r dev/tests/integration/tmp/sandbox-\***

- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/magento-2-integration-tests-with-php-unit&text=Magento%202-%20Integration%20tests%20with%20PHP%20unit>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/magento-2-integration-tests-with-php-unit>
- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/magento-2-integration-tests-with-php-unit&title=&summary=&source=>

### Similar Blog

[![Installing Payment Bridge for Magento enterprise edition](https://blog.e-zest.com/hs-fs/file-3395304159-jpg/blog-files/payment-gateway-option.jpg) Installing Payment Bridge for Magento enterprise edition I’m sharing a very important topic for – ‘Payment Bridge’ for Magento developers By Kailash Lambe Dec 31, 2013](https://blog.e-zest.com/installing-payment-bridge-for-magento-enterprise-edition/)

[![How to set up Apache Virtual Host configuration on LAMP environment?](https://blog.e-zest.com/hs-fs/file-3395200509-jpg/blog-files/apache-virtual-host-configuration-1.jpg) How to set up Apache Virtual Host configuration on LAMP environment? We have often heard people talking about Apache Virtual Host By Kailash Lambe Oct 17, 2012](https://blog.e-zest.com/how-to-set-up-apache-virtual-host-configuration-on-lamp-environment/)

## Helping Organizations Thrive in the Digital Age

[Contact Us](https://www.e-zest.com/contact-us)

- [About](https://www.e-zest.com/about-us)
- [Services](https://www.e-zest.com/our-services)
- [Work](https://www.e-zest.com/our-work)
- [Insights](https://blog.e-zest.com/)
- [Healthcare](https://healthcare.e-zest.com/)
- [Accion Labs](https://www.accionlabs.com/)

- <https://www.linkedin.com/company/e-zest-solutions/>
- <https://twitter.com/ezest>
- <https://www.facebook.com/eZest/>
- <https://www.instagram.com/ezestsolutions/>
- <https://www.youtube.com/user/ezestcorporate>

- Dallas
- Detroit
- Hannover
- Vienna
- London
- Pune

 © 2025 e-Zest Solutions

- [Privacy Policy](https://www.e-zest.com/privacypolicy)
- [Cookie Policy](https://www.e-zest.com/cookie-policy)

 Contact us

 Contact us

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.