how to set up and write unit test

Hi Guys, Today we are going to talk about how to setup and write PHP Unit Tests in Magento 2. As you might know you can write your unit tests for any part of your module for example Block, Controller, Console, Cron, Model, Observer, Plugin, Setup, UI etc. Before we start, we are going to outline the key function of unit test classes with brief explanation.

setUpBeforeClass– This is called once before running all tests in the class

tearDownAfterClass – This is called once after running all tests in the class

setUp – This is called before running a test in the class

tearDown – This is called before running a test in the class

To run all the unit tests for your whole instance you can use the following command

bin/magento dev:tests:run unit

To find out other tests which you can run using the following command

bin/magento dev:tests:run --help

To run all the unit tests for your specific module you can use the following command

php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist vendor/magento/module-catalog/

To run specific unit test you can use the following command

php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist vendor/magento/module-catalog/Test/Unit/Controller/Category/ViewTest.php

Please see the list of PHP Unit Test functions which could be useful for your unit testing

AssertTrue/AssertFalse – Check the input to verify it equals true/false

AssertEquals – Check the result against another input for a match

AssertGreaterThan – Check the result to see if it’s larger than a value (there’s also LessThan, GreaterThanOrEqual, and LessThanOrEqual)

AssertContains – Check that the input contains a certain value

AssertType – Check that a variable is of a certain type

AssertNull – Check that a variable is null

AssertFileExists – Verify that a file exists

AssertRegExp – Check the input against a regular expression

Let’s crack on with our simple example of unit testing

Step 1 – Add your model class with simple calculator functions like addition, substraction, division and multiplication

 namespace ScommerceCustomModel; class Calculator
{ /** * this function will add two numbers * * @param int $numberA * @param int $numberB * @return int */ public function addNumbers ($numberA, $numberB) { return $numberA + $numberB; } /** * this function will subtract two numbers * * @param int $numberA * @param int $numberB * @return int */ public function subtractNumbers ($numberA, $numberB) { return $numberA - $numberB; } /** * this function will divide two numbers * * @param int $numberA * @param int $numberB * @return int */ public function divideNumbers ($numberA, $numberB) { return $numberA / $numberB; } /** * this function will multiply two numbers * * @param int $numberA * @param int $numberB * @return int */ public function multipleNumbers ($numberA, $numberB) { return $numberA * $numberB; } }

Step 2 – Add your test class which will test all the functions which were written in our main calculator class

 namespace ScommerceCustomTestUnitModel; class CalculatorTest extends PHPUnit_Framework_TestCase { /** * @var MagentoFrameworkTestFrameworkUnitHelperObjectManager */ protected $_objectManager; /** * @var ScommerceCustomModelCalculator */ protected $_calculator; ** * unset the variables and objects after use * * @return void */ public function tearDown() { } /** * used to set the values to variables or objects. * * @return void */ public function setUp() { $this->_objectManager = new MagentoFrameworkTestFrameworkUnitHelperObjectManager($this); $this->_calculator = $this->_objectManager->getObject("ScommerceCustomModelCalculator"); } /** * this function will test addition of two numbers * */ public function testAddNumbers () { return $this->assertEquals(10, $this->_calculator->addNumbers(6, 4)); } /** * this function will test substraction of two numbers * */ public function testSubstractNumbers () { return $this->assertEquals(12, $this->_calculator->subtractNumbers(16, 4)); } /** * this function will test division of two numbers * */ public function testDivideNumbers () { return $this->assertEquals(5, $this->_calculator->divideNumbers(20, 4)); } /** * this function will test multiplication of two numbers * */ public function testMultiplyNumbers () { return $this->assertEquals(20, $this->_calculator->MultipleNumbers(5, 4)); }
}

Step 3 – Run your test using the following command

php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Scommerce/Custom/Test/Unit/Model/CalculatorTest.php

You should see the following output -:

 PHPUnit 4.1.0 by Sebastian Bergmann. Configuration read from devtestsunitphpunit.xml.dist Time: 590 ms, Memory: 4.25MB OK (4 tests, 4 assertions) Process finished with exit code 0

That’s it, Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.

Similar Posts