Chapter 11. Testing

Software testing can be defined as a critical step in the development life cycle. This step is often silently overlooked by a number of developers because a certain amount of time need to be invested into writing a decent test suite for a code base. Rather than being a single one-time activity, writing tests is a process that follows our code as it grows and changes. Test results should, at any given time, validate and verify that our software works as expected, thus meeting the business and technical requirements. Writing tests should follow writing the actual application code early on in the life cycle. This helps prevent defects from being introduced in the code.

On a high level, we can divide tests into the following categories:

  • Static: Application code is not executed during testing. Possible errors are found by inspecting the application code files and not on their execution.
  • Dynamic: Application code is executed during testing. Possible errors are found while checking for functional behavior of an application.

In this chapter, we will take a look at the testing options that Magento offers. Along the way, we will build a basic module with some testing features in it.

Types of tests

Magento provides several types of tests out of the box. We can see a list of these tests on running the following command on the console in the Magento root folder:

php bin/magento dev:tests:run –help

The result of the command is an output that looks like this:

Usage:
 dev:tests:run [type]
Arguments:
 type Type of test to run. Available types: all, unit, integration, integration-all, static, static-all, integrity, legacy, default (default: "default")

This output originates from the Console/Command/DevTestsRunCommand.php file in the core Magento_Developer module. Looking at the output, we might say that there are actually nine types of tests, which are as follows:

  • all
  • unit
  • integration
  • integration-all
  • static
  • static-all
  • integrity
  • legacy
  • default

However, these are not unique types of tests; these are combinations, as we will soon see.

Let's take a closer look at the code in the DevTestsRunCommand class and its setupTestInfo method.

The setupTestInfo method defines the internal commands property, as follows:

$this->commands = [
    'unit'                   => ['../tests/unit', ''],
    'unit-performance'       => ['../tests/performance/ framework/tests/unit', ''],
    'unit-static'            => ['../tests/static/ framework/tests/unit', ''],
    'unit-integration'       => ['../tests/integration/ framework/tests/unit', ''],
    'integration'            => ['../tests/integration', ''],
    'integration-integrity'  => ['../tests/integration', ' testsuite/Magento/ Test/Integrity'],
    'static-default'         => ['../tests/static', ''],
    'static-legacy'          => ['../tests/static', ' testsuite/Magento/Test/Legacy'],
    'static-integration-js'  => ['../tests/static', ' testsuite/Magento/Test/ Js/Exemplar'],
];

Furthermore, we can see the types property in the setupTestInfo method defined in the following way:

$this->types = [
    'all'             => array_keys($this->commands),
    'unit'            => ['unit', 'unit-performance', 'unit- static', 'unit-integration'],
    'integration'     => ['integration'],
    'integration-all' => ['integration', 'integration-integrity'],
    'static'          => ['static-default'],
    'static-all'      => ['static-default', 'static-legacy', 'static-integration-js'],
    'integrity'       => ['static-default', 'static-legacy', 'integration-integrity'],
    'legacy'          => ['static-legacy'],
    'default'         => [
        'unit',
        'unit-performance',
        'unit-static',
        'unit-integration',
        'integration',
        'static-default',
    ],
];

The types property logically groups one or more tests into a single name that is found under the commands property. We can see how like unit single type encompasses the unit, unit-performance, unit-static, and unit-integration tests in it. The commands property points to the disk location of the actual test library. Relative to the Magento root installation folder, tests can be found in the dev/tests/ directory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.28.197