With this article, we will explore the PHPUnit testing framework and get started with unit testing. It's a unit testing framework for the PHP programming language. Before code is deployed, unit testing ensures that it meets quality standards. Testing can help developers write better code and save time and money throughout the product development life cycle.
In this article, you will download and install the PHPUnit testing framework and write a basic unit test.
When you're finished, you'll be able to understand how to install the framework and how to write a basic unit test.
Prerequisites
You will need PHP installed as well as a local programming environment set up on your computer.
To set this up, follow the How to Install PHP 7.4 and Set Up a Local Development Environment for your operating system.
Step 1 — Install The PHPUnit Testing Framework
You will need PHP installed as well as a local programming environment set up on your computer.
In this step, we are going to install the PHPUnit testing framework. Run the following command to install the PHPUnit testing framework:
sudo apt install phpunit
You'll see the following output:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libllvm11 libllvm11:i386 shim
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
libapache2-mod-php7.4 libjs-bootstrap4 libjs-d3 libjs-jquery libjs-popper.js
node-jquery php-codecoverage php-deepcopy php-doctrine-instantiator
php-file-iterator php-mbstring php-phar-io-manifest php-phar-io-version
php-phpdocumentor-reflection-common php-phpdocumentor-reflection-docblock
php-phpdocumentor-type-resolver php-phpspec-prophecy php-text-template
php-timer php-token-stream php-tokenizer php-webmozart-assert php-xml
php7.4-cli php7.4-common php7.4-json php7.4-mbstring php7.4-mysql
php7.4-opcache php7.4-readline php7.4-xml phpunit-code-unit-reverse-lookup
phpunit-comparator phpunit-diff phpunit-environment phpunit-exporter
phpunit-global-state phpunit-object-enumerator phpunit-object-reflector
phpunit-recursion-context phpunit-resource-operations phpunit-type
phpunit-version
Suggested packages:
php-pear php-xdebug php-invoker php-soap php-uopz
The following NEW packages will be installed:
libjs-bootstrap4 libjs-d3 libjs-jquery libjs-popper.js node-jquery
php-codecoverage php-deepcopy php-doctrine-instantiator php-file-iterator
php-mbstring php-phar-io-manifest php-phar-io-version
php-phpdocumentor-reflection-common php-phpdocumentor-reflection-docblock
php-phpdocumentor-type-resolver php-phpspec-prophecy php-text-template
php-timer php-token-stream php-tokenizer php-webmozart-assert php-xml
php7.4-mbstring php7.4-xml phpunit phpunit-code-unit-reverse-lookup
phpunit-comparator phpunit-diff phpunit-environment phpunit-exporter
phpunit-global-state phpunit-object-enumerator phpunit-object-reflector
phpunit-recursion-context phpunit-resource-operations phpunit-type
phpunit-version
The following packages will be upgraded:
libapache2-mod-php7.4 php7.4-cli php7.4-common php7.4-json php7.4-mysql
php7.4-opcache php7.4-readline
7 upgraded, 37 newly installed, 0 to remove and 80 not upgraded.
Need to get 7,416 kB of archives.
After this operation, 12.5 MB of additional disk space will be used.
Do you want to continue? [Y/n]
You should select Y and allow all dependencies to download.
Run the following command to make sure the framework is setup correctly:
phpunit --version
You’ll see the following output:
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.
Step 2 — Create Your First Unit Test
Let's start by creating our project in the following directory /home/digitalriver/phpprojects/helloworld-im-unit-testing
Run the following command to create the project directory:
mkdir /home/digitalriver/phpprojects/helloworld-im-unit-testing
Navigate to the created directory:
cd /home/digitalriver/phpprojects/helloworld-im-unit-testing
Let's create the class file we are going to test:
touch helloworld.php
Let's create the test class file that is going to test our class:
touch helloworldtest.php
Modify the contents of the class file using your favorite text editor for this example we are using vi:
vi helloworld.php
Once the text file opens up in the terminal window, type out the following:
<?php
class HelloWorld{
public function sayHello($name){
return "Hello World, ". $name ." started testing with PHPUnit.";
}
}
?>
Save the file contents and quit the text editor.
Modify the contents of the test class file using your favorite text editor for this example we are using vi:
vi helloworldtest.php
Once the text file opens up in the terminal window, type out the following:
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
require 'helloworld.php';
class HelloWorldTests extends TestCase{
private $helloworld;
public function setUp(): void{
$this->helloworld = new HelloWorld();
}
protected function tearDown(): void{
$this->helloworld = null;
}
public function testSayHello(){
$result = $this->helloworld->sayHello("digitalriver");
$this->assertEquals("Hello World, digitalriver started testing with PHPUnit.", $result);
}
}
?>
Save the file contents and quit the text editor.
Step 3 — Run the test
Once you have completed creating the class you want to test and the associated unit test class you want to test the class with you can now run the test using PHPUnit:
phpunit helloworldtest.php
You’ll see the following output:
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)string(48) "Hello World, digitalriver started testing with PHPUnit."
Time: 97 ms, Memory: 4.00 MB
OK (1 test, 1 assertion)
Conclusion
In this article, you should have installed PHPUnit, created a class and associated test case, and run your first test case using PHPUnit. Unit testing has proven valuable in detecting a large percentage of defects during use. Unit testing allows software developers to identify all kinds of issues very early on.
Comments
Post a Comment