How to Run Unit Testing in Magento 2

When you run Unit testing, a set of tests which are defined in the Magento 2 code base will be run. You can choose either run all tests or run the specific tests that you have just chosen. All available types would be terminated and listed by the program whenever there is an unsupported type is specified. In this post, I will offer you the method to run Unit Testing.

How to Run Unit Testing

Step 1: Check the Prerequisites to run unit testing

Before running this Unit Testing, you need to ensure two conditions:

  • Firstly, you need to enable the Magento_Developer module. If it’s hasn’t been activated, you can follow this to enable it:
bin/magento module:enable [--force] Magento_Developer

You should only use the --force option if it’s really necessary.

  • Secondly, to run the desired tests, your system must be set up. For instance, before running the integration tests, the dev/tests/integration/etc/install-config-mysql.php.dist should be copied to dev/tests/integration/etc/install-config-mysql.php and be modified to suit your environment.

Step 2: Run Unit Testing

Use this command:

bin/magento dev:tests:run <test>

to list the test types which are available:

bin/magento dev:tests:run --help

After you done the above stage, it will give you a list which is similar to this following:

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

This is an example when you run integration tests:

bin/magento dev:tests:run integration

Conclusion

Unit Testing plays an important role in validating each unit of the software performs as designed. Therefore knowing how to run Unit Testing could help you find problems early in the development cycle, refactor code or upgrade system libraries as well as help you create better designs.

Enjoyed the tutorial? Spread it to your friends!

magento-2-module-development
run
unit
testing