Integration Testing in Magento 2 (Custom Module)

Step-by-step guide with real examples

What is Integration Testing in Magento 2?

Integration testing in Magento 2 is used to verify that different components of the system (models, resource models, database, APIs, etc.) work together correctly. Unlike unit tests, integration tests interact with the real database and Magento framework.

Why Use Integration Tests?

  • Test database interactions
  • Validate custom module functionality
  • Ensure service contracts work correctly
  • Catch issues missed in unit testing
  • Useful for APIs and repository testing

Integration Test Directory Structure

Integration tests are placed inside the following directory:


dev/tests/integration/testsuite/Vendor/Module/

Step 1: Magento Integration Test Setup

Go to integration test directory:


cd dev/tests/integration

Configure environment file:


cp etc/install-config-mysql.php.dist etc/install-config-mysql.php

Update database credentials in:


dev/tests/integration/etc/install-config-mysql.php

Step 2: Create Basic Integration Test

Create file:


dev/tests/integration/testsuite/Vendor/Module/Model/Test.php


namespace Vendor\Module\Test\Integration\Model;

use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

class Test extends TestCase
{
    public function testSomething()
    {
        $objectManager = Bootstrap::getObjectManager();

        $product = $objectManager->create(\Magento\Catalog\Model\Product::class);

        $this->assertInstanceOf(
            \Magento\Catalog\Model\Product::class,
            $product
        );
    }
}

Step 3: Use Fixtures (Important)

Fixtures are used to prepare test data like products, customers, orders.


/**
 * @magentoDataFixture Magento/Catalog/_files/product_simple.php
 */
public function testLoadProduct()
{
    $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
    $product = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class)
        ->get('simple');

    $this->assertEquals('Simple Product', $product->getName());
}

Step 4: Run Integration Tests


cd dev/tests/integration
php ../../../vendor/bin/phpunit

Step 5: Test Custom Module Logic Example


namespace Vendor\Module\Test\Integration\Model;

use PHPUnit\Framework\TestCase;
use Magento\TestFramework\Helper\Bootstrap;
use Vendor\Module\Model\Custom;

class CustomTest extends TestCase
{
    public function testCustomFunction()
    {
        $objectManager = Bootstrap::getObjectManager();
        $model = $objectManager->create(Custom::class);

        $result = $model->getCustomValue();

        $this->assertEquals("expected_value", $result);
    }
}

Important Annotations

  • @magentoAppIsolation enabled – Isolates app state
  • @magentoDbIsolation enabled – Resets DB after test
  • @magentoDataFixture – Loads test data
  • @magentoConfigFixture – Sets config values

Best Practices

  • Avoid using Object Manager directly (use it only in tests)
  • Use fixtures for consistent data
  • Keep tests independent
  • Use meaningful assertions
  • Clean up after tests using isolation annotations

Common Mistakes

  • Not configuring integration test DB
  • Missing fixtures for required data
  • Hardcoding values without setup
  • Ignoring DB isolation

Interview Question

Q: How do you create an integration test in Magento 2?

Answer: Integration tests are created under dev/tests/integration/testsuite. We use PHPUnit with Magento Test Framework, load fixtures using annotations, and use Bootstrap Object Manager to test real interactions with database and services.

Last updated: March 2026