How to Run the Magento 2 Test Suite?

Magento 2 ships with a complete testing suite out-of-the-box. These include a complete set of all operational unit, integration and static test. The simplest of these to run is probably the u=unit tests, having all necessary components setup and in-place automatically after installing Magento 2 successfully. To strengthen the test suite, Magento uses phpunit, with the test suite configuration residing under ./dev/ in your installation of Magento 2.

Run Unit Tests

Unit tests are the simplest one because there is no requirement of configuration on your part beyond ensuring you have a working copy of phpunit available

Use command below to run only the unit tests:

cd dev/tests/unit/

../../../vendor/phpunit/phpunit/phpunit

Use the command below to run an individual set of tests. In this case we run unit tests for the Magento_catalog module:

cd dev/tests/unit/../../../vendor/phpunit/phpunit/phpunit \  ../../../vendor/magento/module-catalog/Test/Unit/

Run Integration Tests

Running Integration tests is a bit more complicated because for these tests to run, they have to standup a copy of Magento for tests purposes complete with a working database to query against.

  1. Place the following command in dev/tests/integration/etc/install-config-mysql.php:
<?php 
return [   
'db-host' => 'dev-db',   
'db-user' => 'root',   
'db-password' => '',   
'db-name' => 'magento_integration_tests',   
'db-prefix' => '',   
'backend-frontname' => 'backend',   
'admin-user' => \Magento\TestFramework\Bootstrap::ADMIN_NAME,   
'admin-password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,   
'admin-email' => \Magento\TestFramework\Bootstrap::ADMIN_EMAIL,   
'admin-firstname' => \Magento\TestFramework\Bootstrap::ADMIN_FIRSTNAME,   
'admin-lastname' => \Magento\TestFramework\Bootstrap::ADMIN_LASTNAME,
];
  1. Create the database referenced in the above config
mysql -e 'create database magento_integration_tests'
  1. Use command below to run the integration tests:
cd dev/tests/integration/../../../vendor/phpunit/phpunit/phpunit

Run Static Tests

There are 2 ways for you to run static tests like:

cd dev/tests/static/../../../vendor/phpunit/phpunit/phpunit

Or

cd dev/tests/static/framework/tests/unit../../../../../../vendor/phpunit/phpunit/phpunit

Run the Complete Suite

From the root of your installation:

Bin/magento dev:tests:run

That’s all. Hopefully all of you can perform these tests successfully.