Is it worth covering Magento 2 code by automated tests?

The implementation of automated tests has always been an interesting topic for many store owners and managers. We receive many questions every day like: should we invest money for tests in addition to development? How will it help an online business? Which tests are better and why? Therefore, we aim to shed some light on the most popular and useful types of tests and highlight the key reasons for their importance.

A successful online business is not only a website’s user-friendly interface, good Magento 2 SEO, marketing and performance but also its stability. The number of problems arising after development can be reduced by introducing automated tests on a website, especially when talking about ongoing store development. In many cases, it is really hard to predict that any changes made in one part of the website will not lead to issues of functionality in another. Therefore, as a precaution against new bugs being introduced, we recommend implementing automated tests for developed solutions on every website. Let’s check how it works in detail.

Depending on the requirements and project particularities, we recommend investigating the following types of automated tests for Magento 2 websites:

  • Unit tests – this type of test is helpful for testing particular parts (units) of the system without involving the entire system. It’s like testing a car engine separately from the car. Such type of tests allows checking code behavior in different circumstances.
  • Integration tests – check the behavior of separate functional parts of the system. It’s like testing a car cooling system that includes other subsystems like power, airflow, air conditioning in a complex. Integration tests, as a rule, do not involve graphical interface or Magento API interfaces for testing purposes. Integration tests are most helpful for regression testing. The integration tests allow checking different logical scenarios in the system behavior. When we add a new feature, modify another or implement a bug fix, the integration tests should be run in order to make sure that all other parts of the system work as expected.
  • Functional Tests – These tests literally emulate user behavior. Based on the car analogy, it’s like testing different functions of the car by pressing different buttons, tumblers, switchers (like a real user does). So, functional tests usually involve graphical user interface or API endpoints in case of Magento 2 API testing.
  • Static tests – the basic idea of static tests is checking the code style in order to ensure that code meets best practices from a coding standards perspective and does not contain common errors.
  • In case of performance tests, system performance with recent code changes is compared with system performance before changes. In our case with the car, if we change, for example, a type of tires, we can check if any improvements (or slowdown) in speed occurs.

Of course, if a developer creates tests whilst developing a solution himself, in the end, total work requires more time investment. But the huge benefit here is that if we consider the long-term perspective, automated tests provide a possibility to spend less time on overall website code maintenance in future, and in that way, additionally protect the website from new issues. Whenever a new feature is introduced on the website, we can quickly run tests and check if all functionality works as expected, which is, in a way, harder to do with manual testing.

Fortunately, Magento 2 is fully integrated with mentioned tests out of the box. Alongside existing tests that cover core Magento functionality, the system provides a great framework for creating new tests (for custom functionality). In comparison with Magento 1 writing tests for Magento 2 is much easier. So, it is worth considering to use even if extra costs are added to the development. From a long term perspective, automated tests allow saving a lot of time by avoiding manual debugging and bug fixing.

Meanwhile, tests should be maintained and adjusted with almost every new change developed on the store to keep them up-to-date. On the one hand, it’s more time-consuming, but on the other – they are really helpful for detecting any effect from a newly developed change and protecting the store from some unpredictable issues. We would recommend writing and maintaining, at least, integration/functional tests for all website parts or for all functionality that is critical, particularly for business cases.

We hope this brief description of automated tests will help you start using them on your website and improve its functionality.