Magento 2 How to Run Unit Tests via Command Line

In this tutorial, we will talk about How to Run Unit Tests via Command Line in Command Line Series. As you know, from Magento 2, they add many commands in bin/magento. This topic might be difficult to understand but I will make it clear in this post. I will explain in more detail about How to Run Unit Tests via Command Line.

Let’s learn about Magento unit testing first.

What is Unit Testing in Magento 2?

Unit testing is a level of software testing where individual units/components of a software are tested. It is to validate that there is no problem with the performance of each unit of the software.

So why Unit Test?

Here are some benefits of unit testing:

  • Find the problems early
  • Quickly facilitate changes and identify faults caused by changes
  • Test code to identify what it is responsible for to create better design

How to Run Unit Tests via Command Line

Go to Magento admin root folder and show usage:

php bin/magento dev:tests:run --help
Usage:                                                                                                                      
  dev:tests:run [options] [--] [<type>]                                                                                     
                                                                                                                            
Arguments:                                                                                                                  
  type                       Type of test to run. Available types: all, unit, integration, integration-all, static, static-a
ll, integrity, legacy, default [default: "default"]                                                                         
                                                                                                                            
Options:                                                                                                                    
  -c, --arguments=ARGUMENTS  Additional arguments for PHPUnit. Example: "-c'--filter=MyTest'" (no spaces) [default: ""]     
  -h, --help                 Display this help message                                                                      
  -q, --quiet                Do not output any message                                                                      
  -V, --version              Display this application version                                                               
      --ansi                 Force ANSI output                                                                              
      --no-ansi              Disable ANSI output                                                                            
  -n, --no-interaction       Do not ask any interactive question                                                            
  -v|vv|vvv, --verbose       Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug 

Help:                                                                                                                       
  Runs tests

Syntax to Run Unit Tests

php bin/magento dev:tests:run [options]

Simple run tests

php bin/magento dev:tests:run 

Output:

ERRORS!
Tests: 4163, Assertions: 14590, Errors: 6, Failures: 29, Warnings: 1, Skipped: 173, Incomplete: 1.
----------------------------------------------------------------------
FAILED - 4 of 5:
 - dev/tests/unit> /usr/bin/php7.0 ./vendor/phpunit/phpunit/phpunit
 - dev/tests/static/framework/tests/unit> /usr/bin/php7.0 ./vendor/phpunit/phpunit/phpunit
 - dev/tests/integration> /usr/bin/php7.0 ./vendor/phpunit/phpunit/phpunit
 - dev/tests/static> /usr/bin/php7.0 ./vendor/phpunit/phpunit/phpunit

Wrap up

In this article, I have just guided you on how to run unit tests via the command line in Magento 2. I hope it’s helpful for you. If you have any doubts about this topic, feel free to leave a comment below.

Thank you for reading!

Enjoyed the tutorial? Spread it to your friends!

magento-2-module-development
command
line
cli
console

Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.