Setting up and running php unit tests from Eclipse - 1 / 3

We had been make attempts to write automated unit tests in php for some of the business modules.   yii provides a test framework which makes it easy to start writing unit tests.  This uses phpunit.  We had set up phpunit using pear on our systems and could run the tests on our systems from command line.

We had also got the unit tests to run as part of continuous integration and used the test reports and coverage report in sonar.

Recently, we started a new php project and our new developer attempted to set up his sytem to run phpunit.  That was when we discovered a few things.

pear was no longer the popular method to install required php libraries.  It was now composer. phpunit was stopping support for pear and there was no official documentation to install it using pear.

This is the background of this three-part write-up.

In Part I (this write-up), I attempt to detail out how we set up our project for phpunit using composer.
In Part II, I show we set up Eclipse to run the unit tests
In Part III, I will describe how we got the tests to run in our continuous integration and used the data for sonar reports.
  • Step 0: Install php
On Windows, the best bet is to install the wamp since that takes care of bringing together the right set of binaries of apache, php and mysql.   It is way harder to get them working together, if you try to download and set up each of them separately.  (I did try and achieved it, but after facing difficulties upgrading, dropped it)
  • Step 1: Install composer
There is a Windows installer and it does all the magic so that typing composer on command prompt brings up the usage
  • Step 2: Create a composer.json file
This is best placed in the root of the project.  Ours had the following lines (to start with):

    "require": {
        "phpunit/phpunit": "4.1.*",
        "phpunit/phpunit-selenium": ">=1.2",
        "phpunit/phpunit-story": "1.0.2",
        "phpunit/dbunit": "1.3.1"

  • Step 3:  Run the following from the command prompt of your project root (where you have placed composer.json)
composer install

This creates a vendor folder and a bunch of subfolders which contain the libraries/dependencies required to run phpunit.

  • Step 4: Add vendor\bin folder to PATH
The folder contains phpunit.bat Once this is done, phpunit can be run from the command-line.

  • Step 5: Run the tests
Tests can be run in many ways:
  • phpunit <test file>
  • phpunit <test folder>
  • phpunit -c <test-suite-file>
  • Step 6: Checking-in files
As recommended, composer.json and the
composer.lock file that is created should be checked-in to version control.   vendor folder should not be checked in and the best way to ensure it isn't is to add it to the ignore folder list of your version control system (.gitignore, .hgignore, .svnignore).

Now, each developer needs to run all the steps above (save Step 2 and 6) to get the tests to run on his/her system.

Both composer and phpunit have detailed documentation on features and usage and it is best to refer to them for definitive information.


  1. I truly like to reading your post. Thank you so much for taking the time to share such a nice information.

    Website Design Company in Mohali
    Website Development Company in Mohali

  2. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..
    iOS Training in Chennai
    Android Training in Chennai
    php Training in Chennai

  3. Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.
    PHP Training in Chennai


Post a Comment