-1

In my .php file I have more than one function , how I can write test cases for that?

1

Check below code for write test case for any Model

create model file or use existing file

app/code/Vendor/Testcasedemo/Model/Calculator.php

put below code there

namespace Vendor\Testcasedemo\Model;

class Calculator {
    /**
     * this function will perform the addition of two numbers
     *
     * @param float $a
     * @param float $b
     * @return float
     */
    Public function addition($a ,$b) {
        return $a + $b;
    }

    /**
     * this function will perform the addition of two numbers
     *
     * @param float $a
     * @param float $b
     * @return float
     */
    Public function substract($a ,$b) {
        return $a - $b;
    }
}

create test case file

app/code/Vendor/Testcasedemo/Test/Unit/Model

put below code there

namespace Vendor\Testcasedemo\Test\Unit\Model;

class Calculator extends  \PHPUnit\Framework\TestCase {

    protected $_objectManager;
    protected $_desiredResult;
    protected $_actulResult;
    protected $_desiredResult1;
    protected $_actulResult1;
    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 \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
        $this->_calculator = $this->_objectManager->getObject("Vendor\Testcasedemo\Model\Calculator");
        //can do stuff
    }
    /**
     * this function will perform the addition of two numbers
     *
     * @param float $a
     * @param float $b
     * @return float
     */
    public function testAddition() {
         $this->_actulResult = $this->_calculator->addition(7.0,3.0);
         $this->_desiredResult = 10.0;
         $this->assertEquals($this->_desiredResult, $this->_actulResult);
    }

    public function testSubstract() {
         $this->_actulResult1 = $this->_calculator->substract(7.0,3.0);
         $this->_desiredResult1 = 4.0;
         $this->assertEquals($this->_desiredResult1, $this->_actulResult1);
    }
}

As this is just reference code for how to write test case For more details refer below links

1) https://devdocs.magento.com/guides/v2.1/mtf/mtf_entities/mtf_testcase.html

2) https://devdocs.magento.com/guides/v2.2/test/testing.html

3) https://webkul.com/blog/create-custom-module-magento-2-unit-tests/

4) https://inchoo.net/magento-2/unit-testing-magento-2/

1

I hope I understood correctly.
Here is an example:

The class:

class Operation
{
    public function add($a, $b)
    {
         return $a+$b;
    }
    public function subtract($a, $b)
    {
         return $a-$b;
    }
    public function divide($a, $b)
    {
         if ($b == 0) {
             return $a / $b;
         }
         throw new \Exception("Dude...don't divide by zero");
    }
}

The test

class OperationTest
{
     private $operation;
     //this is called before every test. 
     //useful for instantiating the class to test specially when it has dependencies
     protected function setUp()
     {
          $this->operation = new Operation();
     }
     // covers Operation::add()  
     public function testAdd()
     {
         $this->assertEquals(5, $this->operation->add(2,3));
         $this->assertEquals(0, $this->operation->add(2,-2));
         $this->assertEquals(10, $this->operation->add(7,3));
     }
     // covers Operation::subtract()  
     public function testSubtract()
     {
         $this->assertEquals(-1, $this->operation->subtract(2,3));
         $this->assertEquals(4, $this->operation->subtract(2,-2));
         $this->assertEquals(4, $this->operation->subtract(7,3));
     }
     // covers Operation::divide()  
     public function testDivide()
     {
         $this->assertEquals(2, $this->operation->divide(4,2));
         $this->assertEquals(4, $this->operation->add(-8,-2));
     }
     // covers Operation::divide()  when dividing by 0
     // so you can have 2 test methods testing the same class method
     public function testDivideWithException()
     {
         $this->expectException(\Exception::class);
         $this->operation->divide(3, 0);
     }
}

COde not testes. Watch out for syntax errors.

  • Thanks for the replay and for the answer. I was looking for the same – ASHWINI MURTHY Nov 1 '18 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.