🔗 Magento 2 Integration Testing – Test Your Modules in Action
Unit tests are great, but they only test isolated code. Want to see how your code behaves with the Magento system — like database, DI, and config? That’s what integration testing is for! 💥
📁 Where to Place Integration Tests
Magento follows this directory structure for integration tests:
dev/tests/integration/testsuite/Vendor/Module/Model/MyModelTest.php
🛠️ Example Integration Test
Here’s a basic integration test to check if a model works correctly with the Magento object manager and database:
namespace Vendor\Module\Test\Integration\Model; use Magento\TestFramework\Helper\Bootstrap; use PHPUnit\Framework\TestCase; class MyModelTest extends TestCase { public function testModelFunctionality() { $objectManager = Bootstrap::getObjectManager(); $model = $objectManager->create(\Vendor\Module\Model\MyModel::class); $this->assertEquals('Expected Value', $model->getSomething()); } }
▶️ Running Integration Tests
Run your tests using the following command:
cd dev/tests/integration ../../../vendor/bin/phpunit
⚙️ Setup Requirements
- Magento must be installed.
- Integration DB must be configured in
dev/tests/integration/etc/install-config-mysql.php
. - Use
@magentoDbIsolation
and@magentoAppIsolation
annotations if needed.
🧪 Sample Test with Fixture
You can also use Magento’s XML or PHP fixtures to load test data:
/** * @magentoDataFixture Magento/Catalog/_files/product_simple.php */ public function testProductExists() { $objectManager = Bootstrap::getObjectManager(); $product = $objectManager->get(\Magento\Catalog\Model\ProductRepository::class)->get('simple'); $this->assertEquals('Simple Product', $product->getName()); }
✅ Benefits of Integration Testing
- Tests real-life Magento behavior
- Verifies DI, DB access, and events
- Improves module reliability
Integration tests ensure your custom modules not only compile — but actually work in the Magento environment. Time to test like a Magento hero! 🦸