How to Run Unit Testing in Magento 2
In this post, I will demonstrate how to run unit testing in Magento 2.
Unit testing is a level of software testing where individual units/components of software are tested. Unit testing in Magento 2 is useful to validate pieces of code. It helps Magento users make sure that the code quality is at par with the quality standards.
When you run Unit testing, a set of tests that 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.
Now let’s see how it works in particular.
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_Developermodule. 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.distshould be copied to
dev/tests/integration/etc/install-config-mysql.phpand 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
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.
That’s how to tun unit testing in Magento 2. I hope this tutorial is helpful for you.
Thank you for reading!
Enjoyed the tutorial? Spread it to your friends!