PHP – Getting Started PHPUnit test with simple example

PHP – Getting Started PHPUnit test with simple example

In this post we will give you information about PHP – Getting Started PHPUnit test with simple example. Hear we will give you detail about PHP – Getting Started PHPUnit test with simple exampleAnd how to use it also give you demo for it if it is necessary.

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 PHPUnitFrameworkTestCase;


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:

Also see:PHP – Bootstrap autocomplete tokenfield with Ajax Example

php phpunit.phar MyTestTests.php

You will be find this way result:

I hope it can help you…

Hope this code and post will helped you for implement PHP – Getting Started PHPUnit test with simple example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

1  +  4  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US