Learning PHPUnit | Part 1

Learning PHPUnit | Part 1

Posted on:July 20, 2019 at 10:00 AM

I was planning to write a few articles about PHP Unit for the beginner. Today, finally I got a chance to do that. In this series of article, I am going to show you how to use PHP Unit in PHP for the absolute beginner. In this consequence, today I am going to show you how to install PHP Unit with Composer. Let’s dig into it-

First, let’s create a folder called phpunit and open it in your editor as well as terminal. It’s completely empty.

First of all, in your terminal, run the following command to download PHP Unit from the composer.

composer require phpunit/phpunit

Just take note that, I assume the composer is installed on your computer.

Once done, you will see the structure of your directory is like this-

  • vendor [folder]
  • composer.json

Of course, there are many folders and files insider the vendor directory. Just ignore that for time being.

Now in your terminal, if you run vendor/bin/phpunit, then the phpunit should response something at least.

Now, firstly create two folders called tests where normally we will put all of our test files and app where we will put all of our classes. Then in the composer.json file, we need to tell them, there is a folder you need to autoload. The composer.json file should be like that-


    "require": {
        "phpunit/phpunit": "^7.5"
    "autoload": {
    		"App\\": "app"

Then in your terminal, you need to run the following command to update autoloading files.

composer dump-autolod

The next things, you have to create a file called phpunit.xml in your main directory. Then paste the following basic core for phpunit.xml. The file should be like this-


<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
        <testsuite name="Test suite">

Have you done all the parts? Cool.

So far we have done all the settings for PHP Unit. Now, if you run vendor/bin/phpunit in your terminal, you should see a response from the PHP unit at least.

Well, today, I just show you how to install PHPUnit with the composer. In the next episode, I will show you how to write your first test with PHP Unit.