SlideShare a Scribd company logo
1 of 24
PHPUnit
Prepared By:-
Nikunj Bhatnagar
Content
> What is unit Testing
> Introduction of unit testing
> How to use PHPUNIT
> Advantage and Disadvantage
What is unit Testing?
Unit : The smallest testable part of an
application.z
Unit testing : Testing a unit of code isolated from
its dependencies.
Introduction Of PHPUNIT
Testing with PHPUnit means checking that your
program behaves as expected, and performing
a battery of tests, runnable code-fragments that
automatically test the correctness of parts
(units) of the software. These runnable code-
fragments are called unit tests.
Installing PHPUnit
PHPUnit is installed using the PEAR Installer
Commands to install :
pear config-set auto_discover 1
pear install pear.phpunit.de/PHPUnit
Or you can simply download it from git hub and
save it to your htdocs/html forder.Then it will be
ready to use
Writing Tests for PHPUnit
The tests for a class persontest go into a class
persontest.
Persontest inherits (most of the time) from
PHPUnit_Framework_TestCase.
The tests are public methods that are named
test*.
Inside the test methods, assertion methods such
as assertEquals() are used to assert that an
actual value matches an expected value.
Functions
> Define what you expect to happen
> Assertions check statement is true
> 36 assertions as of PHPUnit 3.6
> assertArrayHasKey()
> assertContains()
> assertContainsOnly()
> assertCount()
> assertEmpty()
> assertEquals()
> assertFalse()
Sample PHP class for testing
//persontest.php
<?php
require_once'person1.php';
class persontest extends PHPUnit_framework_TestCase{
public $test;
public function setup(){
$this->test=new person1('nikunj');
}
public function testname(){
$nikunj=$this->test->getname();
$this->assertTrue($nikunj == 'nikunj');
}} ?>
Test class for testing user.php
// person1.php
<?php
class person1{
public $name;
public function _construct($name){
$this->name=$name;
}
public function getname(){
return $this->name;
}}
?>
How to run the PhP unit test case
Firstly Save the persontest.php and person1.php
in your htdocs/html.
Then open your cmd/terminal.
Run command
phpunit UnitTest persontest.php
Runs the tests that are provided by the class
UnitTest. This class is expected to be declared in
the specified sourcefile.
Running our Tests
root@nikunj:/var/www/test-1# phpunit
persontest.php
PHPUnit 3.6.10 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 1.70Mb
OK (1 test, 1 assertion)
For each test run, the PHPUnit command-line
tool prints one character to indicate progress:
> . – Printed when a test succeeds.
> F – Printed when an assertion fails.
> E – Printed when an error occurs while
running the test.
> S – Printed when the test has been skipped.
> I – Printed when the test is marked as being
incomplete.
PHPUnit – Database Extension
PHPUnit Database Extension – DBUnit Port
Can be installed by : pear install phpunit/DbUnit
Currently supported databases:
> MySQL
> PostgreSQL
> Oracle
> SQLite
has access to other database systems such as IBM DB2 or
Microsoft SQL Server Through Zend Framework or Doctrine 2
integrations
The four stages of a database
test
> Set up fixture
> Exercise System Under Test
> Verify outcome
> Teardown
Configuration of a PHPUnit
Database TestCase
Need to Extend abstract TestCase :
PHPUnit_Extensions_Database_TestCase
require_once
'PHPUnit/Extensions/Database/TestCase.php';
class BankAccountDBTest extends
PHPUnit_Extensions_Database_TestCase
{
Configuration of a PHPUnit
Database TestCase
Must Implement
getConnection() - Returns a database connection
wrapper.
getDataSet() - Returns the dataset to seed the
database with.
Implementation of getConnection() and
getDataset() methods
<?php
require_once 'PHPUnit/Extensions/Database/TestCase.p
hp';
class DatabaseTest extends PHPUnit_Extensions_Datab
ase_TestCase{
protected function getConnection(){
$pdo = new PDO('mysql:host=localhost;dbname=te
stdb', 'root', '');return $this-
>createDefaultDBConnection($pdo, 'testdb');
}
protected function getDataSet(){return $this-
>createFlatXMLDataSet(dirname(__FILE__).'/_files/bank
-account-seed.xml');
}}?>
Test class for database testing
//Filename : dbclass.php
<?php
class BankAccount {
public function __construct($accno, $conn, $bal=0) {
$this->addData(array($accno,$bal),$conn);
}function addData($data, $conn) {
$sql = "INSERT INTO bank_account (account_number,
balance) VALUES (:acc,:bal)";
$q = $conn->prepare($sql);
$q->execute(array(':acc'=>$data[0], ':bal'=>$data[1]));
}}
Test case for dbclass.php
<?php
require_once
'PHPUnit/Extensions/Database/TestCase.php';
require_once "dbclass.php";
class BankAccountDBTest extends
PHPUnit_Extensions_Database_TestCase
{
protected $pdo;
public function __construct()
{ $this->pdo = new
PDO('mysql:host=localhost;dbname=phpunitdb', 'root',
'root'); }
protected function getConnection()
{ return $this->createDefaultDBConnection($this->pdo,
'phpunitdb'); }
protected function getDataSet(){
return $this-
>createFlatXMLDataSet('/var/www/tests/bankaccdb/files/see
d.xml'); }
public function testaddData(){
$bank_account = new BankAccount('1234567', $this-
>pdo);
$xml_dataset = $this-
>createFlatXMLDataSet('/var/www/tests/bankaccdb/files/see
d-after-insert.xml');
$this->assertTablesEqual($xml_dataset-
>getTable('bank_account'),$this->getConnection()-
>createDataSet()->getTable('bank_account'));
}}
Running the test
root@nikunj:/var/www/tests/bankaccdb# phpunit
dbclasstest.php
PHPUnit 3.6.10 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 3.00Mb
OK (1 test, 1 assertion)
Advantages
•Testing gives code authors and reviewers confidence
that patches produce the correct results.
•Detect errors just after code is written.
•The tests are run at the touch of a button and present
their results in a clear format.
•Tests run fast.
•The tests do not affect each other. If some changes are
made in one test, the results of others tests do not
change.
Disadvantages
Some people have trouble with getting
started: where to put the files, how big the
scope of one unit test is and when to write a
separate testing suite and so on.
It would be difficult to write a test for people
who are not programmers or familiar with
PHP.
Thank You
Question. . . .?
You can leave comment
or
Send the message.

More Related Content

What's hot (20)

Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
Unit testing - An introduction
Unit testing - An introductionUnit testing - An introduction
Unit testing - An introduction
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
05 junit
05 junit05 junit
05 junit
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
Junit
JunitJunit
Junit
 
JMockit Framework Overview
JMockit Framework OverviewJMockit Framework Overview
JMockit Framework Overview
 
PHPUnit - Unit testing
PHPUnit - Unit testingPHPUnit - Unit testing
PHPUnit - Unit testing
 
TestNG
TestNGTestNG
TestNG
 
Python unit testing
Python unit testingPython unit testing
Python unit testing
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
Ui test 자동화하기 - Selenium + Jenkins
Ui test 자동화하기 - Selenium + JenkinsUi test 자동화하기 - Selenium + Jenkins
Ui test 자동화하기 - Selenium + Jenkins
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
Unit test
Unit testUnit test
Unit test
 
Test automation
Test automationTest automation
Test automation
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With Pytest
 
TestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKETestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKE
 

Similar to Phpunit testing

Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnitvaruntaliyan
 
Unit Testing in PHP
Unit Testing in PHPUnit Testing in PHP
Unit Testing in PHPRadu Murzea
 
EPHPC Webinar Slides: Unit Testing by Arthur Purnama
EPHPC Webinar Slides: Unit Testing by Arthur PurnamaEPHPC Webinar Slides: Unit Testing by Arthur Purnama
EPHPC Webinar Slides: Unit Testing by Arthur PurnamaEnterprise PHP Center
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitJames Fuller
 
13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboards13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboardsDenis Ristic
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit TestingMike Lively
 
Getting started with PHPUnit
Getting started with PHPUnitGetting started with PHPUnit
Getting started with PHPUnitKhyati Gala
 
Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014Eric Hogue
 
Unlock The Mystery Of PHPUnit (Wave PHP 2018)
Unlock The Mystery Of PHPUnit (Wave PHP 2018)Unlock The Mystery Of PHPUnit (Wave PHP 2018)
Unlock The Mystery Of PHPUnit (Wave PHP 2018)ENDelt260
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09Michelangelo van Dam
 
Pengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDDPengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDDtlabamazing
 
Unit testing for WordPress
Unit testing for WordPressUnit testing for WordPress
Unit testing for WordPressHarshad Mane
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2Tricode (part of Dept)
 
Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Michelangelo van Dam
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentationThanh Robi
 

Similar to Phpunit testing (20)

Zend Framework 2 - PHPUnit
Zend Framework 2 - PHPUnitZend Framework 2 - PHPUnit
Zend Framework 2 - PHPUnit
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
 
Php unit (eng)
Php unit (eng)Php unit (eng)
Php unit (eng)
 
Phpunit
PhpunitPhpunit
Phpunit
 
Unit Testing in PHP
Unit Testing in PHPUnit Testing in PHP
Unit Testing in PHP
 
EPHPC Webinar Slides: Unit Testing by Arthur Purnama
EPHPC Webinar Slides: Unit Testing by Arthur PurnamaEPHPC Webinar Slides: Unit Testing by Arthur Purnama
EPHPC Webinar Slides: Unit Testing by Arthur Purnama
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnit
 
13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboards13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboards
 
Unit testing
Unit testingUnit testing
Unit testing
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
Getting started with PHPUnit
Getting started with PHPUnitGetting started with PHPUnit
Getting started with PHPUnit
 
Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014
 
Unlock The Mystery Of PHPUnit (Wave PHP 2018)
Unlock The Mystery Of PHPUnit (Wave PHP 2018)Unlock The Mystery Of PHPUnit (Wave PHP 2018)
Unlock The Mystery Of PHPUnit (Wave PHP 2018)
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09
 
Pengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDDPengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDD
 
Unit testing for WordPress
Unit testing for WordPressUnit testing for WordPress
Unit testing for WordPress
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
 
Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentation
 

Recently uploaded

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

Phpunit testing

  • 2. Content > What is unit Testing > Introduction of unit testing > How to use PHPUNIT > Advantage and Disadvantage
  • 3. What is unit Testing? Unit : The smallest testable part of an application.z Unit testing : Testing a unit of code isolated from its dependencies.
  • 4. Introduction Of PHPUNIT Testing with PHPUnit means checking that your program behaves as expected, and performing a battery of tests, runnable code-fragments that automatically test the correctness of parts (units) of the software. These runnable code- fragments are called unit tests.
  • 5. Installing PHPUnit PHPUnit is installed using the PEAR Installer Commands to install : pear config-set auto_discover 1 pear install pear.phpunit.de/PHPUnit Or you can simply download it from git hub and save it to your htdocs/html forder.Then it will be ready to use
  • 6. Writing Tests for PHPUnit The tests for a class persontest go into a class persontest. Persontest inherits (most of the time) from PHPUnit_Framework_TestCase. The tests are public methods that are named test*. Inside the test methods, assertion methods such as assertEquals() are used to assert that an actual value matches an expected value.
  • 7. Functions > Define what you expect to happen > Assertions check statement is true > 36 assertions as of PHPUnit 3.6 > assertArrayHasKey() > assertContains() > assertContainsOnly() > assertCount() > assertEmpty() > assertEquals() > assertFalse()
  • 8. Sample PHP class for testing //persontest.php <?php require_once'person1.php'; class persontest extends PHPUnit_framework_TestCase{ public $test; public function setup(){ $this->test=new person1('nikunj'); } public function testname(){ $nikunj=$this->test->getname(); $this->assertTrue($nikunj == 'nikunj'); }} ?>
  • 9. Test class for testing user.php // person1.php <?php class person1{ public $name; public function _construct($name){ $this->name=$name; } public function getname(){ return $this->name; }} ?>
  • 10. How to run the PhP unit test case Firstly Save the persontest.php and person1.php in your htdocs/html. Then open your cmd/terminal. Run command phpunit UnitTest persontest.php Runs the tests that are provided by the class UnitTest. This class is expected to be declared in the specified sourcefile.
  • 11. Running our Tests root@nikunj:/var/www/test-1# phpunit persontest.php PHPUnit 3.6.10 by Sebastian Bergmann. . Time: 0 seconds, Memory: 1.70Mb OK (1 test, 1 assertion)
  • 12. For each test run, the PHPUnit command-line tool prints one character to indicate progress: > . – Printed when a test succeeds. > F – Printed when an assertion fails. > E – Printed when an error occurs while running the test. > S – Printed when the test has been skipped. > I – Printed when the test is marked as being incomplete.
  • 13. PHPUnit – Database Extension PHPUnit Database Extension – DBUnit Port Can be installed by : pear install phpunit/DbUnit Currently supported databases: > MySQL > PostgreSQL > Oracle > SQLite has access to other database systems such as IBM DB2 or Microsoft SQL Server Through Zend Framework or Doctrine 2 integrations
  • 14. The four stages of a database test > Set up fixture > Exercise System Under Test > Verify outcome > Teardown
  • 15. Configuration of a PHPUnit Database TestCase Need to Extend abstract TestCase : PHPUnit_Extensions_Database_TestCase require_once 'PHPUnit/Extensions/Database/TestCase.php'; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase {
  • 16. Configuration of a PHPUnit Database TestCase Must Implement getConnection() - Returns a database connection wrapper. getDataSet() - Returns the dataset to seed the database with.
  • 17. Implementation of getConnection() and getDataset() methods <?php require_once 'PHPUnit/Extensions/Database/TestCase.p hp'; class DatabaseTest extends PHPUnit_Extensions_Datab ase_TestCase{ protected function getConnection(){ $pdo = new PDO('mysql:host=localhost;dbname=te stdb', 'root', '');return $this- >createDefaultDBConnection($pdo, 'testdb'); } protected function getDataSet(){return $this- >createFlatXMLDataSet(dirname(__FILE__).'/_files/bank -account-seed.xml'); }}?>
  • 18. Test class for database testing //Filename : dbclass.php <?php class BankAccount { public function __construct($accno, $conn, $bal=0) { $this->addData(array($accno,$bal),$conn); }function addData($data, $conn) { $sql = "INSERT INTO bank_account (account_number, balance) VALUES (:acc,:bal)"; $q = $conn->prepare($sql); $q->execute(array(':acc'=>$data[0], ':bal'=>$data[1])); }}
  • 19. Test case for dbclass.php <?php require_once 'PHPUnit/Extensions/Database/TestCase.php'; require_once "dbclass.php"; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { protected $pdo; public function __construct() { $this->pdo = new PDO('mysql:host=localhost;dbname=phpunitdb', 'root', 'root'); }
  • 20. protected function getConnection() { return $this->createDefaultDBConnection($this->pdo, 'phpunitdb'); } protected function getDataSet(){ return $this- >createFlatXMLDataSet('/var/www/tests/bankaccdb/files/see d.xml'); } public function testaddData(){ $bank_account = new BankAccount('1234567', $this- >pdo); $xml_dataset = $this- >createFlatXMLDataSet('/var/www/tests/bankaccdb/files/see d-after-insert.xml'); $this->assertTablesEqual($xml_dataset- >getTable('bank_account'),$this->getConnection()- >createDataSet()->getTable('bank_account')); }}
  • 21. Running the test root@nikunj:/var/www/tests/bankaccdb# phpunit dbclasstest.php PHPUnit 3.6.10 by Sebastian Bergmann. . Time: 0 seconds, Memory: 3.00Mb OK (1 test, 1 assertion)
  • 22. Advantages •Testing gives code authors and reviewers confidence that patches produce the correct results. •Detect errors just after code is written. •The tests are run at the touch of a button and present their results in a clear format. •Tests run fast. •The tests do not affect each other. If some changes are made in one test, the results of others tests do not change.
  • 23. Disadvantages Some people have trouble with getting started: where to put the files, how big the scope of one unit test is and when to write a separate testing suite and so on. It would be difficult to write a test for people who are not programmers or familiar with PHP.
  • 24. Thank You Question. . . .? You can leave comment or Send the message.