PHPUnit is a software testing framework released under a GPL license for PHP programmers. It’s popular among PHP developers to write custom tests for their code to measure its performance. This way they can see if their app or code written in PHP programming language can withstand different scenarios and how they behave in some changes.
PHPUnit can help you with:
- Allow isolated testing of a specific part of PHP code.
- Let’s see if the code can work as expected under different conditions.
- Helps identify problems before your users do, ultimately saving time and money debugging later.
- Ensure backward compatibility between versions of your software.
Why use it?
PHPUnit provides an automated diagnostic approach to unit testing, saving developers a lot of time and effort in error detection. In addition, automating tests not only saves time, but also reduces maintenance costs through faster feedback cycles. Therefore, the issues can be found earlier in the development lifecycle or in future upgrades.
Not only time and money, but also testing the code beforehand ensures the security of the software. Ensuring security and data integrity in testing, rather than just focusing on specific areas addressed by human reviewers who may not address more subtle elements that could ultimately prove disastrous if left unaddressed.
Steps to Install PHPUnit on Ubuntu 22.04 | 20.04 Linux
There are three ways to install PHPUnit on Linux: one is by downloading the required version of this testing framework directly from the official website and the other is by using APT and with the help of PHP Composer.
1. Update Ubuntu Linux
Go to your Ubuntu command terminal and start running the system update command. This will install the latest available security and application packages.
sudo apt update && sudo apt upgrade
2. Install some PHP dependencies
In order to run PHPUnit to test code, the PHP language needs to be installed on your system along with some dependencies/extensions as shown in the following command:
sudo apt install php-cli php-json php-mbstring php-xml php-pcov php-xdebug
#1. Method using the APT package manager:
3. Install PHPUnit on Ubuntu 22.04 | 20.04
The long-term supported version of the PHPUnit software testing framework can be installed into Ubuntu via the default system repository and the APT packager manager. Therefore, all you have to do in your command terminal is type:
sudo apt install phpunit

#2. Method using PHPUnit.phar
4. Download and configure Phar from PHPUbnit
In this method, we download the available PHPUnit directly phar Format for easy execution on Linux systems including Ubuntu. Compared to APT method, here we can download and use both latest and old versions of PHPUnit if needed.
In your terminal, use the given command. When I wrote the article, the latest stable version was PHPunit-10. If you want PHPunit-11 or PHPUnit-9, substitute the version number in the given command.
wget -O phpunit.phar https://phar.phpunit.de/phpunit-10.phar
Make the downloaded phar file executable:
chmod +x phpunit.phar
Move it to the local bin directory so we can use it anywhere from the terminal.
sudo mv phpunit.phar /usr/local/bin/phpunit
Reload bash session:
newgrp
Check the PHPUnit version
phpunit --version
#3. Method using PHP Composer
5. Install PHP Composer
For those who already have Composer on their system or want to use it PHP composer To install PHPUnit, do the following:
Download the Composer installation script:
curl -sS https://getcomposer.org/installer -o composer-setup.php
Start the installation process:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
6. Install PHPUnit with Composer
Once the composer is on your system, run:
composer global require phpunit/phpunit
To use PHPUnit globally after installing it via Composer, use:
echo 'export PATH="$PATH:$HOME/.config/composer/vendor/bin"' >> ~/.bashrc
Reload session
newgrp
7. Test some PHP code
Let’s confirm PHPUnit works by testing some simple PHP code. Here it is:
nano first.php
Add the following code:
In class First” First is the name of file. So replace it if your code file name is different.
<?php
class Rectangle {
public $width;
public $height;
public function area() {
return $this->width * $this->height;
}
}
// PHPUnit test
class first extends \PHPUnit\Framework\TestCase {
public function testArea() {
$rectangle = new Rectangle();
$rectangle->width = 5;
$rectangle->height = 10;
$this->assertEquals(50, $rectangle->area());
}
}
Save the file by pressing Ctrl+X, Yand then Input Button.
Now test your code with PHPUnit
phpunit first.php
Exit:

For more information, see the official documentation.
uninstall
If you want to completely remove PHPUnit from your Ubuntu Linux, then follow the same method you used to install it and uninstall it as well.
For the APT method:
sudo apt autoremove --purge phpunit
For PHAR
sudo rm /usr/local/bin/phpunit
newgrp
For composers
composer remove phpunit/phpunit
Other articles
0 Comments