Magento 2 Testing
Before writing code
Fetch live docs:
- Web-search
site:developer.adobe.com commerce testing for testing overview
- Fetch
https://developer.adobe.com/commerce/testing/functional-testing-framework/ for MFTF guide
- Web-search
site:developer.adobe.com commerce php development test for unit/integration test patterns
Testing Layers
Unit Tests
Purpose: Test individual classes in isolation.
- Location:
Test/Unit/ within the module
- Config:
dev/tests/unit/phpunit.xml.dist
- Run:
vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/VendorName/ModuleName/Test/Unit/
- Mock all dependencies with PHPUnit mocks or
createMock()
- No Magento bootstrap — pure PHP unit testing
- Fast execution, no database or filesystem dependencies
Integration Tests
Purpose: Test module interactions with the Magento framework.
- Location:
Test/Integration/ or dev/tests/integration/
- Config:
dev/tests/integration/phpunit.xml.dist
- Run:
vendor/bin/phpunit -c dev/tests/integration/phpunit.xml.dist
- Uses Magento application bootstrap — real DI, real database
@magentoDbIsolation enabled — rolls back DB changes after each test
@magentoAppArea — sets the application area (frontend, adminhtml)
@magentoConfigFixture — sets config values for the test
@magentoDataFixture — loads test data
- Slower but tests real interactions
MFTF (Magento Functional Testing Framework)
Purpose: End-to-end browser tests.
- XML-based test definitions (not PHP)
- Converts to Codeception PHP tests
- Uses Selenium WebDriver for browser automation
- Allure reporting for results
- Location:
Test/Mftf/Test/, Test/Mftf/ActionGroup/, Test/Mftf/Page/, Test/Mftf/Section/
- Run:
vendor/bin/mftf run:test <TestName>
- Tests complete user workflows (add to cart, checkout, admin operations)
API Functional Tests
Purpose: Test REST and SOAP API endpoints.
- Location:
dev/tests/api-functional/
- Config:
dev/tests/api-functional/phpunit_rest.xml.dist
- Tests API request/response contracts
- Verifies authentication, authorization, and data integrity
Static Tests
Purpose: Code quality and standards.
- Location:
dev/tests/static/
- Checks: coding standards (PHP_CodeSniffer + Magento standard), dependency validation, copyright headers
- Run:
vendor/bin/phpunit -c dev/tests/static/phpunit.xml.dist
Key Testing Patterns
Mocking in Magento
- Use
$this->createMock(ClassName::class) for dependencies
$mock->expects($this->once())->method('getById')->willReturn($entity)
- For ObjectManager-dependent classes: use
ObjectManagerHelper from test framework
Test Data Fixtures
Integration tests use fixtures to set up test state:
@magentoDataFixture annotation points to a fixture file
- Fixture files create entities (products, customers, orders)
@magentoDbIsolation rolls back after test
Testing Plugins
Unit test plugin classes independently — call the plugin method with a mock subject and verify behavior.
Testing Observers
Unit test observer's execute() method with a mocked Observer/Event object.
Best Practices
- Write unit tests for all business logic classes
- Use integration tests for DI wiring and database interaction verification
- Use MFTF for critical user journeys (checkout, customer registration)
- Run static tests in CI/CD to enforce coding standards
- Use
@magentoDbIsolation to prevent test pollution
- Keep unit tests fast — mock all external dependencies
- Name test methods descriptively:
testSaveThrowsExceptionWhenNameIsEmpty()
Fetch the testing documentation for exact annotations, fixture patterns, and MFTF XML schema before writing tests.
1---2name: magento-testing3description: Write tests for Magento 2 — PHPUnit unit tests, integration tests, MFTF functional tests, and API tests. Use when implementing test coverage for modules, debugging, or setting up CI/CD test pipelines.4allowed-tools: Read, Write, Edit, Bash, Grep, Glob, WebSearch, WebFetch5metadata:6 source: https://skillmd.com/skills/orcaqubits/magento-testing7---89# Magento 2 Testing1011## Before writing code1213**Fetch live docs**:141. Web-search `site:developer.adobe.com commerce testing` for testing overview152. Fetch `https://developer.adobe.com/commerce/testing/functional-testing-framework/` for MFTF guide163. Web-search `site:developer.adobe.com commerce php development test` for unit/integration test patterns1718## Testing Layers1920### Unit Tests2122**Purpose**: Test individual classes in isolation.2324- Location: `Test/Unit/` within the module25- Config: `dev/tests/unit/phpunit.xml.dist`26- Run: `vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/VendorName/ModuleName/Test/Unit/`27- Mock all dependencies with PHPUnit mocks or `createMock()`28- No Magento bootstrap — pure PHP unit testing29- Fast execution, no database or filesystem dependencies3031### Integration Tests3233**Purpose**: Test module interactions with the Magento framework.3435- Location: `Test/Integration/` or `dev/tests/integration/`36- Config: `dev/tests/integration/phpunit.xml.dist`37- Run: `vendor/bin/phpunit -c dev/tests/integration/phpunit.xml.dist`38- Uses Magento application bootstrap — real DI, real database39- `@magentoDbIsolation enabled` — rolls back DB changes after each test40- `@magentoAppArea` — sets the application area (frontend, adminhtml)41- `@magentoConfigFixture` — sets config values for the test42- `@magentoDataFixture` — loads test data43- Slower but tests real interactions4445### MFTF (Magento Functional Testing Framework)4647**Purpose**: End-to-end browser tests.4849- XML-based test definitions (not PHP)50- Converts to Codeception PHP tests51- Uses Selenium WebDriver for browser automation52- Allure reporting for results53- Location: `Test/Mftf/Test/`, `Test/Mftf/ActionGroup/`, `Test/Mftf/Page/`, `Test/Mftf/Section/`54- Run: `vendor/bin/mftf run:test <TestName>`55- Tests complete user workflows (add to cart, checkout, admin operations)5657### API Functional Tests5859**Purpose**: Test REST and SOAP API endpoints.6061- Location: `dev/tests/api-functional/`62- Config: `dev/tests/api-functional/phpunit_rest.xml.dist`63- Tests API request/response contracts64- Verifies authentication, authorization, and data integrity6566### Static Tests6768**Purpose**: Code quality and standards.6970- Location: `dev/tests/static/`71- Checks: coding standards (PHP_CodeSniffer + Magento standard), dependency validation, copyright headers72- Run: `vendor/bin/phpunit -c dev/tests/static/phpunit.xml.dist`7374## Key Testing Patterns7576### Mocking in Magento7778- Use `$this->createMock(ClassName::class)` for dependencies79- `$mock->expects($this->once())->method('getById')->willReturn($entity)`80- For ObjectManager-dependent classes: use `ObjectManagerHelper` from test framework8182### Test Data Fixtures8384Integration tests use fixtures to set up test state:85- `@magentoDataFixture` annotation points to a fixture file86- Fixture files create entities (products, customers, orders)87- `@magentoDbIsolation` rolls back after test8889### Testing Plugins9091Unit test plugin classes independently — call the plugin method with a mock subject and verify behavior.9293### Testing Observers9495Unit test observer's `execute()` method with a mocked Observer/Event object.9697## Best Practices9899- Write unit tests for all business logic classes100- Use integration tests for DI wiring and database interaction verification101- Use MFTF for critical user journeys (checkout, customer registration)102- Run static tests in CI/CD to enforce coding standards103- Use `@magentoDbIsolation` to prevent test pollution104- Keep unit tests fast — mock all external dependencies105- Name test methods descriptively: `testSaveThrowsExceptionWhenNameIsEmpty()`106107Fetch the testing documentation for exact annotations, fixture patterns, and MFTF XML schema before writing tests.