For Create a new module with Bluethink Testing follow this blog. If you follow this blog then you can easily implement new module with Testing.
Step 1: Create registration.php file.
app/code/Bluethink/Testing/registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Bluethink_Testing', __DIR__ ); |
Step 2: Create module.xml file.
app/code/Bluethink/CustomJS/etc/module.xml
1 2 3 4 5 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Bluethink_Testing" setup_version="1.0.0"> </module> </config> |
Step 3: Create Unittest.php file.
app/code/Bluethink/Testing/Model/Unittest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php namespace Bluethink\Testing\Model; class Unittest { /** * this function will perform the addition of two numbers * * @param int $no1 * @param int $no2 * @return int */ Public function additiondata($no1 ,$no2) { return $no1 + $no2; } } |
Step 4: Create Unittest.php file
app/code/Bluethink/Testing/Test/Unit/Model/Unittest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php namespace Bluethink\Testing\Test\Unit\Model; class Unittest extends \PHPUnit\Framework\TestCase { public function setUp() : void { $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_calculator = $this->_objectManager->getObject("Vendor\Extension\Model\Unittest"); } /** * this function will perform the addition of two numbers * * @param int $no1 * @param int $no2 * @return int */ public function testcaseAddition(): void { $this->_actulResult = $this->_calculator->additiondata(7,7); // call additiondata method from model file $this->_desiredResult = 14; // actual result compare set $this->assertEquals($this->_desiredResult, $this->_actulResult); /* check test case match or not if true the give ok message otherwise give error */ } } |
How to Run Unit Test in Magento 2?
Test all the Unit test in Magento Core modules
php bin/magento dev:tests:run unit
Unit test for a specific module.
php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Bluethink/Testing
Unit Test for a specific file
php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Bluethink/Testing/Test/Unit/Model/Unittest.php
bluethinkinc_blog
2022-09-30