PHP - Getting Started PHPUnit Test Example
PHPUnit is a one type of unit testing framework for PHP language. In todays PHPUnit is very popular for testing. Most of site owner want to implement PHPUnit test because that way we can simply test using command. Without testing our website will never be good to publish. But if you don't know about PHPUnit and no idea how to start so just follow bellow example for scratch.
I will give you very simple example of PHPUnit and how it works from scratch. I make quick easy example with phpunit command. We have to create two file like as listed bellow:
1)MyTest.php
2)MyTestTests.php
Before start example make sure you have installed PHP. So you have to just follow bellow files to complete example.
Download PHPUnit
wget https://phar.phpunit.de/phpunit.phar
php phpunit.phar --version
Create MyTest.php File
<?php
class MyTest
{
public function add($a, $b)
{
return $a + $b;
}
}
Create MyTestTests.php File
<?php
require 'MyTest.php';
use PHPUnit\Framework\TestCase;
class MyTestTests extends TestCase
{
private $mytest;
protected function setUp()
{
$this->mytest = new MyTest();
}
protected function tearDown()
{
$this->mytest = NULL;
}
public function testAdd()
{
$result = $this->mytest->add(1, 3);
$this->assertEquals(4, $result);
}
}
Now we are ready to run bellow simple example by following example:
php phpunit.phar MyTestTests.php
You will be find this way result:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Disable Submit Button After Form Submission in PHP?
- PHP JQuery Ajax Post Request Example
- PHP MySQL Create Dynamic Treeview Example
- PHP JQuery Chosen Ajax Autocomplete Example
- How to Create Zip Folder and Download in PHP?
- PHP Bootstrap Autocomplete Tokenfield using Ajax Example
- Tags Input with Autocomplete using jQuery and PHP Example
- Laravel Ajax Image Upload with Validation Example
- PHP MySQL Highcharts Chart Example
- PHP Behance API Tutorial with Example
- PHP Capture Screenshot of Website from URL Example
- PHP JQuery Select2 Ajax Autocomplete Example
- File Upload with Progress Bar using jQuery Ajax and PHP Example
- PHP Check Word Exist in String or Not Example