Taction Software

info@tactionsoftware.com sales.taction

Unit Testing With PHPUnit Testing

Unit Testing With PHPUnit Testing

Even a small project on development may have complex logic. During a development process, major and minor bugs may arrive and problems creep in the structure. Then bugs are resolved in the development process by the developer.

There is no sure-fire way to develop a bug-free system. Another important aspect is the possibility of prompting of new bugs when existing bugs are fixed.

A browser is able to give the syntax/fatal error but what if in case of some logical error. PHPUnit testing enables a developer to check his/her logic. It is an entirely object-oriented framework.


Unit testing means checking that your program behaves as expected, performing a battery of tests, runnable code-fragments that automatically test the correctness of parts (units) of the software. These runnable code-fragments are called unit tests.
In this way, PHPUnit testing checks the logic and behavior of the project.

Installing PHPUnit:

PHPUnit is a testing framework for PHP program which helps developer should find error in the newly committed code. It is much like other unit frameworks & we can install PHPUnit in our system with the help of following two steps.

  1. Installing using composer:- To install PHPUnit from Composer, run the following command in SSH terminal.
    $ composer require “phpunit/phpunit=5.5.*”
  2. If you are using windows you can simply download it from git hub and save it to your htdocs/HTML folder. Then it will be ready to use

How to write test Cases?

With PHPUnit, the most basic thing you’ll write is a test case. A test case is just a term for a class with several different tests all related to the same functionality. There are a few rules you’ll need to worry about when writing your cases so that they’ll work with PHPUnit: Firstly, create a PHP file let’s say mytest.php the file need to inherit the class PHPUnit_framework_TestCase the library of PHPUnit.

The code looks like:-

public function testname(){
$this->assertTrue($testcase== ‘myfirst-test’);
One more class we need to create as person.php

The code looks like:-

public function getname(){
return $this->name;

:- We have used a function assertTrue which is known as Assertion in PHPUnit.There is 36 assertion in PHPUnit 3.6.T his function checks whether both the values are equal or not.

How to run the PHPUnit test case?

To run PHPUnit test cases we need to open the cmd/terminal and we need to define the path where our file exists. We need to run command.
PHPUnit UnitTest persontest.php

the command line will follow the command and result will appear

The result looks like:-

root@nikunj:/var/www/test-1# PHPUnit persontest.php

PHPUnit 3.6.10 by Sebastian Bergmann.

Time: 0 seconds, Memory: 1.70Mb

OK (1 test, 1 assertion)

Explanation:- For each test run, the PHPUnit command-line tool prints one character to indicate progress:

  • . – Printed when a test succeeds.
  • F – Printed when an assertion fails.
  • E – Printed when an error occurs while running the test.
  • S – Printed when the test has been skipped.
  • I – Printed when the test is marked as being incomplete.

Our test succeeds and it also tells the memory used and a number of test cases performed as we have tested one assertion. So it shows 1 test 1 assertion.

• Testing gives code authors and reviewers confidence that patches produce the correct results.

• Detect errors just after the code is written.

• The tests are run at the touch of a button and present their results in a clear format.

• Tests run fast.

• The tests do not affect each other. If some changes are made in one test, the results of others tests do not change.

• Some people have trouble getting started: Where to put the files, how big the scope of one unit test and when to write a separate testing suite and so on.

• It would be difficult to write a test for people who are not programmers or familiar with PHP.

WhatsApp chat