Vitalyi Golomoziy - Integration tests in Magento 2
-
Upload
meet-magento-italy -
Category
Presentations & Public Speaking
-
view
48 -
download
3
Transcript of Vitalyi Golomoziy - Integration tests in Magento 2
ABOUT MYSELF
• Architect and SME for Magento U
• Working in Magento for more than 8 years
TABLE OF CONTENTS
• Automated testing in Magento 2
• Integration tests framework overview
• Writing a simple integration test
AUTOMATED TESTING IN MAGENTO 2
QUALITY OF THE AUTOMATED TEST
FragilityAccuracy
Savings Costs
AUTOMATED TESTING IN MAGENTO 2
• Unit testing
• Integration testing
• Functional testing
UNIT TEST
• TDD
• Better code structure
• Execution of every line of the
code
Fragility Accuracy
FUNCTIONAL TEST
• Business scenarios testing
• Ensuring the whole system is
working as expected
• Edge case in user’s behavior
testing
Accuracy Fragility
INTEGRATION TEST
• Code-level feature testing
• Testing feature connected to the
infrastructure
Accuracy Fragility
INTEGRATION TESTS FRAMEWORK OVERVIEW
FRAMEWORK
• Based on phpunit
• Provides magento-specific annotations
• Provides a lot of magento-specific tools
• Special object manager object
CONFIGURATION
• etc/install-config-mysql.php
• etc/config-global.php
• etc/di/preferences/(ce.php|ee.php)
• phpunit.xml
USEFUL ANNOTATIONS
• @magentoDataFixture
• @magentoAppIsolation
• @magentoDbIsolation
• @magentoAppArea
• @magentoCache
• @magentoConfigFixture
MAGENTO TEST CASES
• AbstractController
• AbstractBackendController
• AbstractConfigFile
• AbstractIntegrity
ADD SHARED INSTANCE
\PHPUnit_Framework_MockObject_MockObject $transportBuilderMock,
$originalClassName
) {
$userMock = $this->_objectManager->create(
$originalClassName,
['transportBuilder' => $transportBuilderMock]
);
$factoryMock = $this->getMockBuilder('Magento\User\Model\UserFactory')
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$factoryMock->method('create')
->willReturn($userMock);
$this->_objectManager->addSharedInstance(
$factoryMock,
'Magento\User\Model\UserFactory'
);
}
WRITING A SIMPLE INTEGRATION TEST
CONFIGURATION
return [
'db-host' => 'localhost',
'db-user' => ’__database_user__',
'db-password' => ’__password__',
'db-name' => ’special_db_for_integration_tests',
'db-prefix' => '',
'backend-frontname' => 'admin',
'admin-user' => \Magento\TestFramework\Bootstrap::ADMIN_NAME,
'admin-password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
'admin-email' => \Magento\TestFramework\Bootstrap::ADMIN_EMAIL,
'admin-firstname' => \Magento\TestFramework\Bootstrap::ADMIN_FIRSTNAME,
'admin-lastname' => \Magento\TestFramework\Bootstrap::ADMIN_LASTNAME,
];
FIXTURES
<?php
global $students;
$students = [
[’name' => ’John’, ‘surname’ => ‘Doe’],
[’name' => ’Another John’, ‘surname’ => ‘Doe’],
];
TEST
/**
* @magentoDataFixture __path_to_fixture_file.php
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
* @magentoAppArea adminhtml
*/
public function testCRUD()
{
global $students;
$model = $this->studentsFactory>create();
$model->setName($students[0][’name’])
->setSurname($students[0][‘surname’]);
$crud = new \Magento\TestFramework\Entity($model);
$crud->testCrud();
}
Q&A
THANK YOU FOR ATTENTION