Magento Testing
Magento (both a free, open-source version and a commerce version called Adobe Commerce) is a popular choice for building e-commerce websites. It is a powerful platform with its own testing frameworks to help you test your website. However, there are certain limitations when it comes to testing that you ought to know.
- Unit Testing
- Integration Testing
- Magento Functional Testing Framework (MFTF)
- End-to-End Testing
Magento Unit Testing
As the testing pyramid suggests, unit tests should be at the very beginning, encompassing the base of all tests. Magento supports unit testing of all algorithms and methods. It utilizes the PHPUnit framework. It supports PHP as well as JavaScript unit testing. Since Magento has some predefined features, it also comes with some predefined unit test cases. These unit tests are written by following the Test Data Driven (TDD) approach.
Ways to Execute Magento PHP Unit Tests
Magento unit tests can be executed in the following ways.
- Command Line Interface (CLI)
It is possible to run all or a select few tests using the CLI. Their specific directory paths need to be mentioned for the respective cases to be executed. For further reading, refer to this link. - PhpStorm IDE
It is also possible to run unit tests through IDEs like PhpStorm. Just like the CLI execution, the IDE execution also supports running either all or a select group of unit tests. However, the IDE needs to be configured to run the correct set of cases. Further details on the configuration and commands can be found here.
Ways to Execute Magento JavaScript Unit Tests
Magento provides sophisticated frontend capabilities for frontend unit testing. The Commerce version of Magento supports testing JavaScript code using Jasmine and a custom version of Grunt CLI task runner. Further details on the configuration and commands can be found here.
Magento Integration Testing
The next layer after unit tests is typically integration. As you know, the goal of these tests is to make sure various components interact with each other as expected. Some examples include integration with a third-party system or an API. Keep in mind that integration tests in Magento require a runtime environment.
- HTTP tests to test APIs
- Third party integration tests
- Browser tests (although you might want to prefer them to be a part of end-to-end tests)
All of the details on setting up the integration testing with Magento can be found here.
<?php namespace IntegerNet\Categorydisplay\Test\Integration\Controller; class NewProductTest extends \Magento\TestFramework\TestCase\AbstractController { public static function loadFixture() { include __DIR__ . '/../_files/categories.php'; include __DIR__ . '/../_files/products.php'; } /** * @magentoDbIsolation enabled * @magentoAppIsolation enabled * @magentoDataFixture loadFixture */ public function testDisplaysCorrectProduct() { $this->dispatch('catalog/category/view/id/xyz'); $this->assertContains('OurProduct 1', $this->getResponse()->getBody()); $this->assertNotContains('Our Product 2', $this->getResponse()->getBody()); } }
Magento End-to-end Testing
End-to-end tests sit on top of the testing pyramid. They are typically the slowest (compared to other types of tests) but also the most representative. Since E2E tests represent the entire user flow, having a tight coverage is the only way to ensure a great user experience.
We won't get into Selenium-based testing here since it might not be the most optimal way to use for end-to-end testing due to its limitations.
Magento Functional Testing Framework (MFTF)
MFTF aims to simplify the testing of complex use cases that are likely to be encountered in e-commerce websites. It tends to target the pages that the end users and admins of the system will interact with. As your website grows, it becomes essential to have automation testing strategies in place to validate the integrity of the ever-growing e-commerce website constantly. This framework is quite stable as it is based on technologies like Selenium, Allure, and Codeception. Since it is meant for Magento websites, it is highly customizable and scalable. It is open source which makes it a popular choice for Magento applications testing. As Magento provides some common functionalities that are used in e-commerce websites, it also has a directory where functional test cases for these features are stored.
It is necessary to set up the environment for functional testing by downloading the required versions of PHP, Java, Composer, and web driver. A detailed overview of this setup can be found here.
All test cases are written in XML. The XML files are built to generate PHP class files which are then executed. It is possible to add assertions, data sets, and club repetitive actions into action groups using MFTF. Details on how to convert manual test cases into MFTF cases can be found here.
Reports of these functional test cases are generated using Allure.
testRigor End-to-end Testing
testRigor specializes in end-to-end testing. Among the key features are ease of test creation, unrivaled test stability, and low maintenance - a rare combination for E2E tests.
- Support email testing, as well as testing for attachments as part of the tests
- Support visual testing if needed (ex: you can compare an entire screen to a previous version)
- Test two-factor authentication logins on Gmail, SMS, etc
End-to-end tests in testRigor represent how a real user would interact with the application. What this means is that you don't need to use CSS Selectors or dig into the DOM structure to define an element. Commands are in simple English terms, such as click "Add to Cart", or check that checkbox "Remember me" is checked.
click "Sign in" enter "Joe" into "Username" enter "JoePass" into "Password" click "Verify me" check that sms to "+12345678902" is delivered and matches regex "Code\:\d\d\d\d" and save it as "sms" extract value by regex "(?<=Code\:)[0-9]{4}" from "sms" and save it as "confirmationCode" enter saved value "confirmationCode" into "code" click "Continue to Login" check that page contains text "Welcome, Joe!"
enter "Jack" into table "Client Info" at row "11" and column "First Names"
The best part is this test will work regardless of being rendered as div-based or as an HTML table.
As you can see, it's very straightforward to create end-to-end tests covering the entire user flows while keeping abstraction from the details of implementation.