Magento unit testing basic setup

Magento unit testing basic setup © 123dan321@sxc.hu

In my first Magento post I mentioned importance of using unit testing during development.
Somebody will say: ‘Hello, I don’t have a time to make some weird unit tests, and why should I need them anyway?’
I will not try to explain why this is necessary here, because any good experienced developer that wants to follow best practice rules will know what I am talking about….

Let’s use my current scenario:

I am currently extending core and developing custom Magento web service API calls to fit our project needs. Do I need to test the calls somehow or I will leave testing to the mobile side of the story?
From my existing experience it’s more logical for me to test each call to API before I publish them to development server for testing on mobile side. Imagine that for each line of code you write, you have to re-publish source again and again an even you are not sure that it is working properly …
What a waste of precious development time …

Let’s create our testing environment:

First of all, installing phpUnit is neccesary. Install PEAR (you will find how-to instructions on their website), and also install phpUnit first.

With these packages installed, fire-up your favorite IDE (I am using NetBeans 7.0.1) and make some lightweight setup for unit testing:

Open:

Tools/Options and select ‘Php‘ tab.
Here you will find text field where you should insert location path for yours phpUnit script. In my case it is located in:
C:\WampDeveloper\Components\Php\PEAR\phpunit.bat‘, but if yours localhost installation is somehow different, you should find it inside php/PEAR folder of yor local web server installation.

You have plenty of ways to configure the way the tests will be executed, but for our purpose it will be enough to right click on your project inside ‘Projects’ window and go to ‘Properties/phpUnit’ where we will choose option: ‘Run all *Test files using PHPUnit ‘ and press OK.

Next step is to write/put some test files and configure them to run within Magento:

Create new folder inside your webroot in your project and call it for example: ‘Tests’.
Inside that folder – create new Php class called for example: ‘customerCustomerTest.php’:

<?php
// customerCustomerTest.php
 
require_once 'PHPUnit/Autoload.php';
require_once '../app/Mage.php';
 
class customerCustomerTest extends PHPUnit_Framework_TestCase {
 
    public function setUp() {
        Mage::app('default');
    }
 
}

Right click on our ‘Tests’ folder and choose Tools/create phpUnit tests, and choose path to out ‘Tests’ folder from file browser window. And we are done with basic set-up and we can start to write tests.

Of course – from above code it’s obvious that you will have to add phpUnit library under global include path if it isn’t the case already ….

Now you can write your first test with Magento.
In next article I will make some web service API unit tests with SoapClient, so if you are interested, stay tuned … 🙂

You made it all the way down here so you must have enjoyed this post! You may also like:

How To Connect Google Analytics 4 To Magento 2 Bojan Mareljic
Bojan Mareljic, | 35

How To Connect Google Analytics 4 To Magento 2

Setup scripts in Magento 2 Ivan Weiler
Ivan Weiler, | 11

Setup scripts in Magento 2

Magento API V2 Soap unit testing Darko Goles
Darko Goles, | 3

Magento API V2 Soap unit testing

2 comments

  1. We would like to have unit test for our magento Extension, please give ome guidelines to write the unit test script.

  2. I have installed phpUnit in Xampp and its location is C:\xampp\php\PEAR\PHPUnit.
    But I couldn’t find C:\xampp\php\PEAR\phpunit.bat nor C:\xampp\php\PEAR\phpUnit\PHPUnit/Autoload.php files.
    There was no error while installing it.
    Now how can I set it up to run the test.
    Thanks!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.