Install PHPUnit and run unit test cases

Hello Guys,

Today we will discuss the PHPUnit and try to make a program to perform unit testing using PHP Language.

PHPUnit is complete unit testing framework written in the PHP Programming Language and was written by Sebastian Bergmann. It allows to PHP programmers to perform unit testing and practice test driven Development(TDD).

For installation of the PHPUnit on Linux based system like on Ubuntu, I used following steps

If it will show you the version of PHPUnit then you will confirm that PHPUnit is installed successfully and you can access it globally from anywhere in the system.

Now it’s time to creating the first PHPUnit test class.

Create a folder named ‘phpunit’ for unit testing in the php web development folder i.e /var/www/html/ .

Now create your first unit test class file. I have given the name ‘MyTest.php’ and write the below code

We need to extend out test class from TestCase parent class.
When we will start to write the methods then every method will start with prefix ‘test’ and they will be public methods.

Now our class has been created and we need to test it.

Open the Terminal and reach folder /var/www/html/phpunit then PHPUnit command line test runner can be invoked through the phpunit command. The below code shows how to run tests with the PHPUnit command-line test runner:

When invoked, as shown above, the PHPUnit command-line test runner will look for a MyTest.php source file, load it, and expect to find a MyTest test case class. It will then execute the tests of that class and you will get the output shown as below

Basically, assertsEquals function are used to assert that actual value matches an expected value. assertFalse will check the value is false or not. It will return pass if we provide false otherwise, it returns true.
assertTrue will also check for the true value.

Thanks for Reading.

Leave a Comment

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

Optimization WordPress Plugins & Solutions by W3 EDGE