Back to Top

Unit testing in magento 1

Updated 6 January 2018

Developers who follows best practices of coding write unit testing in their extensions. We will see the basics of how to setup and use PHP unit testing in magento 1.

Setup

Go to your root parallel to your ‘app‘ folder from your terminal and paste the following command-

composer require phpunit/phpunit

This will install PHPUnit in your server

unit testing

Searching for an experienced
Magento Company ?
Find out More

Our setup is completed.

Implementation

Create a xml file phpunit.xml parallel to your app folder and write the following code there.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
        colors="true"
        verbose="true"
        stopOnFailure="false">
    <testsuites>
        <testsuite name="My Test Suite">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

Inside <directory> the path to the test files is mentioned. Now create a folder named ‘tests‘ as mentioned in the xml file parallel to app folder. Inside the tests folder create a PHP file Test.php (any name)

In the Test.php file write the following code.

<?php
require_once './app/Mage.php';
require_once './app/code/local/Webkul/Module/Helper/Data.php';
class Test extends \PHPUnit\Framework\TestCase {

    /** @test */
    public function test_function() {
        $data = new Webkul_Module_Helper_Data();

        $this->assertEquals($data->getTotal(200),200);

    }

}

assertEquals checks if the two parameters are equal or not. Here we are calling a function getTotal() in Data.php file inside Helper. If that function returns value 200 then we will see the following output

output

Use this code in terminal to check output

./vendor/bin/phpunit

Hope this helps. Happy Coding.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • Erik
    • Paul Dutta (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home