Monday, June 15, 2026
HomeLanguagesPHPUnit | Test framework for php

PHPUnit | Test framework for php

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. 
It is used for the purpose of unit testing for PHP code. PHPUnit was created by Sebastian Bergmann and its development is hosted on GitHub.
Purpose: Its purpose is to verify functionality and impact of newly written code by developers. By running the unit test cases a developer can easily find mistakes in their business logic or functionality of the previously written code. PHPUnit uses assertions to verify that the behavior of the specific component.
The goal of unit testing is to isolate each part of the program and show that the individual parts are correct. A unit test provides a strict, written contract that the piece of code must satisfy. As a result, unit tests find problems early in the development cycle. A developer can use different types of assertions for different types of expected results and hence can verify them easily. For assertion, PHPUnit provides a different function to assert actual output versus expected output.
 

  • For installing DS in php follow the steps mentioned here.
  • For using phpunit follow the steps from here
     

Note : Although the code looks like of php but can’t be compiled on php compiler. Use phpunit filename.php command to run the code at local machine.
 

PHP




<? php
// Use PHPunit Framework
use PHPUnit\Framework\TestCase;
 
 
// Extend the test case class of phpunit
class StackTest extends TestCase
{
    public function testPushAndPop()
   {
 
// create an empty vector
$vector = new \Ds\Vector();
 
// assert the size of vector
$this->assertSame(0, count($vector));
$vector->insert(0, "first");
// assert the value of vector
$this->assertSame('first', $vector[count($vector)-1]);
 
// assert the size of vector
$this->assertSame(1, count($vector));
 
// pop and assert the popped element
$this->assertSame('first', $vector->pop());
$this->assertSame(0, count($vector));
    }
}
?>


Output: 

PHPUnit 6.5.5 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 827 ms, Memory: 4.00MB

OK (1 test, 5 assertions)

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS