Published On: June 6th, 20243.6 min read

Developers are often seen to use some testing methods in their software development process. Software testing helps them to test the quality of the product or service. It’s very important to minimize the risk of broken functionalities on software enhancement or modification. Software engineers generally use white-box testing at the unit level.

There are several testing levels. Unit testing is one of them. It is also known as component testing. It is used to verify the functionality of a specific section of code, generally at the function level.

To ensure the specific function is working as expected is the main purpose of using this test. Only unit testing cannot check the functionality of a piece of software. There might be multiple tests for catching corner cases or other branches in the code of a single function. Rather it is used to assure that the building blocks that are used in software work independently of each other.

Which Programming Language supports Unit Testing?

Many programming languages support unit testing directly. Such as-

  • C#

  • Java

  • Ruby

  • Python

  • Corba

  • ABAP

  • D

  • Obix

PHPUnit

PHPUnit is a unit test framework package for PHP. If we want to write unit tests in PHP we have to integrate the PHPUnit testing framework into php because there is no built-in testing framework in PHP language.

Behavior of the PHPUnit framework

A framework such as PHPUnit has to resolve a set of constraints, some of which seem always to conflict with each other. Tests have some advantages. Such as:

1.     It’s easy to learn to write.

2.     Tests are easy to write. So developers do not ignore to write them.

3.     Tests code is easy to read.

4.     It’s easy to execute. The tests should run at the touch of a button and present their results in a clear and unambiguous format.

5.     It runs fast. So developers can easily run the test hundreds or thousands of times a day.

6.     The tests do not affect each other. If the order in which the tests are run changes, the results of the tests do not change.

How to install PHPUnit?

First, open a terminal and update pear (this is the facility that we’ll use to install PHPUnit):

sudo pear config-set auto_discover 1

sudo pear install pear.phpunit.de/PHPUnit

sudo pear install phpunit/DbUnit

How to Use?

From the following table, you can see how to write tests using PHPUnit that exercise PHP’s array operations. This example introduces the basic conventions and steps for writing tests with PHPUnit:

  1. The tests for a class Class go into a class ClassTest.

  2. Most of the time ClassTest inherits from PHPUnit_Framework_TestCase.

  3. The tests are public methods that are named test*. Otherwise, you can use the @test annotation in a method’s doc block to mark it as a test method.

  4. Inside the test methods, assertion methods such as assertEquals() (see the section called “Assertions”) are used to assert that an actual value matches an expected value.

Our main php coding file Calculator.php:


This is our simple Calculator class with a single function add that will return the summation of two numbers provided by a user. Now can write test code for this class it will be better if we write our test code into a separate directory for example myapp/unit/testcodefile.php. I have created a unit test file called TestCalculator.php.

objCalculator =  new Calculator();
}
public function testAddFunctionWithValidData()
{
$knownResult = 7;
$expectedResult = $this->objCalculator->add(5, 2);
$this->assertEquals($knownResult, $expectedResult);
}
public function testAddFunctionWithInValidData()
{
$expectedResult = $this->objCalculator->add(‘a’, 2);
$this->assertFalse($expectedResult)
}
}
?>

There are many assertions in the PHP unit some of which are:

  • assertCount(0, array(‘foo’))

  • AssertEmpty(array())

  • assertFalse(TRUE)

  • AssertGreaterThanOrEqual(2, 1)

  • assertNull(‘foo’)

  • assertEquals(‘bar’, ‘baz’)

  • AssertSame(‘2204’, 2204)

For running the unit test go to the terminal then go to your unit test directory myapp/unit/ and then type unittest yourfile.php. Here is the output of our unit test result

PHPUnit 3.7.0 by Sebastian Bergmann.
..
Time: 0 seconds, Memory: 3.50Mb

Tests: 2, Assertions: 2, Failures: 0.

If there is something wrong with your add function then the test will fail and display an error message for each assertion.

So, this is the way you can use PHPUnit testing for the PHP programming language.

Contributor: Mehreen Chowdhury, Marketing Executive, Nascenia

Share it, Choose Your Platform!

More to Explore

The Quest for Knowledge Continues. Fuel Your Curiosity.