SlideShare uma empresa Scribd logo
1 de 31
Baixar para ler offline
Unit Testing for WordPress
Harshad Mane
PHP Developer
WordPress Enthusiast

harshadmane.in

@harshadmane
No one writes a PERFECT CODE :)
Software Testing?
It’s impossible to guarantee a bug-free release, but you
can minimize the chances by using automated tests.
There’s nothing better than ending your day with a
clean conscience knowing you did your job right.
Test cases are executed manually without any support
from tools or scripts.
Manual Testing
Test cases are executed with the assistance of tools,
scripts, and software.
Automated Testing
Manual/Automated Testing: Pros and Cons
Manual Automated
Not accurate. More reliable
Time Consuming Process Faster than a manual approach.
It does not involve in programming
task to fetch hidden information.
Testers can test complicated
application too.
Useful in UI Testing Not always!
Automated software testing can increase the depth and
scope of tests to help improve software quality.
Why do we need automation testing?
Lengthy tests that are often avoided during manual
testing can be run unattended.
A unit test is a piece of code that exercises another
piece of code.
What are Unit Tests?
A unit test should check that one piece of code, such as
a function or method, runs properly and returns the
expected results.
How to setup unit tests for your code.
We will walk through
How to write the unit tests.
How to run the tests.
How to write a testable code.
Let’s look quickly at a simple example
function hm_get_vat( $quantity )
{
return $quantity * 0.15;
}
A good unit test for our hm_get_vat() function would try
passing in various numbers in various formats to
determine if the results are always what we expect them
to be.
A unit test is nothing more than a programmatic check
to determine if a chunk of code performs the way it is
supposed to.
public function test_hm_get_vat()
{
$obj = new Class;
$vat = $obj->hm_get_vat(100);
$this->assertEquals( 15, $vat );
}
Setting up the Testing Suite
Easiest way to setup Unit Tests is through WP-CLI
WP-CLI, a command line interface for WordPress.
http://wp-cli.org/
https://make.wordpress.org/cli/handbook/installing/
1. Install PHPUnit
https://github.com/sebastianbergmann/
phpunit#installation
2. Install WP-CLI
https://make.wordpress.org/cli/handbook/plugin-unit-
tests/
1. Go to your WordPress install root folder
cd /Applications/MAMP/htdocs/phpunit/
2. Instruct WP-CLI to create the initial unit test files
wp scaffold plugin-tests <plugin-slug>
This will generate all of the files needed for our unit
tests.
wp scaffold child-theme Generate child theme based on an existing theme.
wp scaffold plugin Generate starter code for a plugin.
wp scaffold plugin-tests Generate files needed for running PHPUnit tests in a plugin.
wp scaffold post-type Generate PHP code for registering a custom post type.
wp scaffold taxonomy Generate PHP code for registering a custom taxonomy.
wp scaffold theme-tests Generate files needed for running PHPUnit tests in a theme.
wp scaffold _s Generate starter code for a theme based on _s.
https://developer.wordpress.org/cli/commands/scaffold/
The new folders / files created:
bin/
install-wp-tests.sh
tests/
bootstrap.php
test-sample.php
phpunit.xml
.travis.yml
These files are the foundation of our plugin’s
test suite.
bash bin/install-wp-tests.sh phpunit_test root
root localhost latest
Above command will create a test database and install
all test dependencies.
The testing suite that WP-CLI sets up for us includes
one sample unit test
It is located in tests/test-sample.php:
class SampleTest extends WP_UnitTestCase {
function testSample() {
// replace this with some actual testing code
$this->assertTrue( true );
}
}
Writing your First Test
phpunit.xml
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite>
<directory prefix="test-" suffix=".php"d>./tests/
</directory>
</testsuite>
</testsuites>
</phpunit>
This tells PHPUnit where to look for the PHP file
tests/bootstrap.php defines a few options, and then
also tells PHPUnit where the actual unit tests live
Here files which are prefixed with “test-“ are only
considered
Note: only methods prefixed with “test” will be
considered a unit test. All other methods will be
skipped.
Things to note down while writing tests
Separate the tests into multiple files so that they are
logically organized
Every class contains an extension of the
WP_UnitTestCase class.
The name of the class does not really matter, just name
it something that makes sense.
Assertions
An assertion is one “check” within the the test to
determine if a value is equal to what we expect.
Tests
Group of one or more assertions are Tests
One test with a single assertion:
function test_ktm_message() {
$string = ‘WordCamp Kathmandu 2017';
$this->assertEquals( 'WordCamp Kathmandu 2017',
$string );
}
One test with a two assertions:
function test_ktm_message() {
$string = ‘WordCamp Kathmandu 2017 - Awesome Event‘;
$this->assertEquals( 'WordCamp Kathmandu 2017 - Awesome
Event', $string );
$this->assertNotEquals( 'WordCamp Kathmandu 2017',
$string );
}
https://phpunit.de/manual/current/en/appendixes.assertions.html
Q&A
Thank you!
धन्यवाद :)

Mais conteúdo relacionado

Mais procurados

Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkArulalan T
 
CI / CD w/ Codeception
CI / CD w/ CodeceptionCI / CD w/ Codeception
CI / CD w/ CodeceptionTudor Barbu
 
Functional Testing Swing Applications with Frankenstein
Functional Testing Swing Applications with FrankensteinFunctional Testing Swing Applications with Frankenstein
Functional Testing Swing Applications with Frankensteinvivek_prahlad
 
Automation testing with Drupal 8
Automation testing with Drupal 8Automation testing with Drupal 8
Automation testing with Drupal 8nagpalprachi
 
Pengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDDPengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDDtlabamazing
 
Why Windows 8 drivers are buggy
Why Windows 8 drivers are buggyWhy Windows 8 drivers are buggy
Why Windows 8 drivers are buggyPVS-Studio
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialJAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialAnup Singh
 
Top 25 Selenium Interview Questions and Answers 2018
Top 25 Selenium Interview Questions and Answers 2018Top 25 Selenium Interview Questions and Answers 2018
Top 25 Selenium Interview Questions and Answers 2018Testbytes
 
Drupalcamp Simpletest
Drupalcamp SimpletestDrupalcamp Simpletest
Drupalcamp Simpletestlyricnz
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails AppsRabble .
 
Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8Sam Becker
 
Five Easy Ways to QA Your Drupal Site
Five Easy Ways to QA Your Drupal SiteFive Easy Ways to QA Your Drupal Site
Five Easy Ways to QA Your Drupal SiteMediacurrent
 
Automated Testing in Django
Automated Testing in DjangoAutomated Testing in Django
Automated Testing in DjangoLoek van Gent
 
JAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineJAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineAnup Singh
 
Front end unit testing using jasmine
Front end unit testing using jasmineFront end unit testing using jasmine
Front end unit testing using jasmineGil Fink
 

Mais procurados (20)

Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-framework
 
CI / CD w/ Codeception
CI / CD w/ CodeceptionCI / CD w/ Codeception
CI / CD w/ Codeception
 
Functional Testing Swing Applications with Frankenstein
Functional Testing Swing Applications with FrankensteinFunctional Testing Swing Applications with Frankenstein
Functional Testing Swing Applications with Frankenstein
 
Automation testing with Drupal 8
Automation testing with Drupal 8Automation testing with Drupal 8
Automation testing with Drupal 8
 
Unit testing
Unit testingUnit testing
Unit testing
 
Pengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDDPengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDD
 
Why Windows 8 drivers are buggy
Why Windows 8 drivers are buggyWhy Windows 8 drivers are buggy
Why Windows 8 drivers are buggy
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialJAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
 
Top 25 Selenium Interview Questions and Answers 2018
Top 25 Selenium Interview Questions and Answers 2018Top 25 Selenium Interview Questions and Answers 2018
Top 25 Selenium Interview Questions and Answers 2018
 
Test
TestTest
Test
 
Drupalcamp Simpletest
Drupalcamp SimpletestDrupalcamp Simpletest
Drupalcamp Simpletest
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails Apps
 
Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8
 
Zend Framework 2 - PHPUnit
Zend Framework 2 - PHPUnitZend Framework 2 - PHPUnit
Zend Framework 2 - PHPUnit
 
Five Easy Ways to QA Your Drupal Site
Five Easy Ways to QA Your Drupal SiteFive Easy Ways to QA Your Drupal Site
Five Easy Ways to QA Your Drupal Site
 
Automated Testing in Django
Automated Testing in DjangoAutomated Testing in Django
Automated Testing in Django
 
Drupal 7 ci and testing
Drupal 7 ci and testingDrupal 7 ci and testing
Drupal 7 ci and testing
 
JAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineJAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & Jasmine
 
Selenium Handbook
Selenium HandbookSelenium Handbook
Selenium Handbook
 
Front end unit testing using jasmine
Front end unit testing using jasmineFront end unit testing using jasmine
Front end unit testing using jasmine
 

Semelhante a 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_v2Tricode (part of Dept)
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit TestingMike Lively
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnitMindfire Solutions
 
Upstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testingUpstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testingDanWooster1
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to heroJeremy Cook
 
Joomla! Testing - J!DD Germany 2016
Joomla! Testing - J!DD Germany 2016Joomla! Testing - J!DD Germany 2016
Joomla! Testing - J!DD Germany 2016Yves Hoppe
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitJames Fuller
 
Unit Testing in PHP
Unit Testing in PHPUnit Testing in PHP
Unit Testing in PHPRadu Murzea
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And DrupalPeter Arato
 
Selenium-Webdriver With PHPUnit Automation test for Joomla CMS!
Selenium-Webdriver With PHPUnit Automation test for Joomla CMS!Selenium-Webdriver With PHPUnit Automation test for Joomla CMS!
Selenium-Webdriver With PHPUnit Automation test for Joomla CMS!Puneet Kala
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnitvaruntaliyan
 
Pragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScriptPragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScriptdavejohnson
 
Testing with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifeTesting with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifePeter Gfader
 

Semelhante a Unit testing for WordPress (20)

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
 
PHPUnit
PHPUnitPHPUnit
PHPUnit
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
 
Upstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testingUpstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testing
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to hero
 
Joomla! Testing - J!DD Germany 2016
Joomla! Testing - J!DD Germany 2016Joomla! Testing - J!DD Germany 2016
Joomla! Testing - J!DD Germany 2016
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnit
 
Unit Testing in PHP
Unit Testing in PHPUnit Testing in PHP
Unit Testing in PHP
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And Drupal
 
Selenium-Webdriver With PHPUnit Automation test for Joomla CMS!
Selenium-Webdriver With PHPUnit Automation test for Joomla CMS!Selenium-Webdriver With PHPUnit Automation test for Joomla CMS!
Selenium-Webdriver With PHPUnit Automation test for Joomla CMS!
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
 
Phpunit
PhpunitPhpunit
Phpunit
 
Php unit (eng)
Php unit (eng)Php unit (eng)
Php unit (eng)
 
Pragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScriptPragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScript
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Testing with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifeTesting with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs Life
 
Unit test
Unit testUnit test
Unit test
 
What is selenium
What is seleniumWhat is selenium
What is selenium
 
Python and test
Python and testPython and test
Python and test
 

Último

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 

Último (20)

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 

Unit testing for WordPress

  • 1.
  • 2. Unit Testing for WordPress
  • 3. Harshad Mane PHP Developer WordPress Enthusiast
 harshadmane.in
 @harshadmane
  • 4. No one writes a PERFECT CODE :) Software Testing? It’s impossible to guarantee a bug-free release, but you can minimize the chances by using automated tests. There’s nothing better than ending your day with a clean conscience knowing you did your job right.
  • 5. Test cases are executed manually without any support from tools or scripts. Manual Testing
  • 6. Test cases are executed with the assistance of tools, scripts, and software. Automated Testing
  • 7. Manual/Automated Testing: Pros and Cons Manual Automated Not accurate. More reliable Time Consuming Process Faster than a manual approach. It does not involve in programming task to fetch hidden information. Testers can test complicated application too. Useful in UI Testing Not always!
  • 8. Automated software testing can increase the depth and scope of tests to help improve software quality. Why do we need automation testing? Lengthy tests that are often avoided during manual testing can be run unattended.
  • 9. A unit test is a piece of code that exercises another piece of code. What are Unit Tests? A unit test should check that one piece of code, such as a function or method, runs properly and returns the expected results.
  • 10. How to setup unit tests for your code. We will walk through How to write the unit tests. How to run the tests. How to write a testable code.
  • 11. Let’s look quickly at a simple example
  • 12. function hm_get_vat( $quantity ) { return $quantity * 0.15; }
  • 13. A good unit test for our hm_get_vat() function would try passing in various numbers in various formats to determine if the results are always what we expect them to be.
  • 14. A unit test is nothing more than a programmatic check to determine if a chunk of code performs the way it is supposed to.
  • 15. public function test_hm_get_vat() { $obj = new Class; $vat = $obj->hm_get_vat(100); $this->assertEquals( 15, $vat ); }
  • 16. Setting up the Testing Suite
  • 17. Easiest way to setup Unit Tests is through WP-CLI WP-CLI, a command line interface for WordPress. http://wp-cli.org/ https://make.wordpress.org/cli/handbook/installing/
  • 18. 1. Install PHPUnit https://github.com/sebastianbergmann/ phpunit#installation 2. Install WP-CLI https://make.wordpress.org/cli/handbook/plugin-unit- tests/
  • 19. 1. Go to your WordPress install root folder cd /Applications/MAMP/htdocs/phpunit/ 2. Instruct WP-CLI to create the initial unit test files wp scaffold plugin-tests <plugin-slug> This will generate all of the files needed for our unit tests.
  • 20. wp scaffold child-theme Generate child theme based on an existing theme. wp scaffold plugin Generate starter code for a plugin. wp scaffold plugin-tests Generate files needed for running PHPUnit tests in a plugin. wp scaffold post-type Generate PHP code for registering a custom post type. wp scaffold taxonomy Generate PHP code for registering a custom taxonomy. wp scaffold theme-tests Generate files needed for running PHPUnit tests in a theme. wp scaffold _s Generate starter code for a theme based on _s. https://developer.wordpress.org/cli/commands/scaffold/
  • 21. The new folders / files created: bin/ install-wp-tests.sh tests/ bootstrap.php test-sample.php phpunit.xml .travis.yml These files are the foundation of our plugin’s test suite.
  • 22. bash bin/install-wp-tests.sh phpunit_test root root localhost latest Above command will create a test database and install all test dependencies.
  • 23. The testing suite that WP-CLI sets up for us includes one sample unit test It is located in tests/test-sample.php: class SampleTest extends WP_UnitTestCase { function testSample() { // replace this with some actual testing code $this->assertTrue( true ); } }
  • 26. This tells PHPUnit where to look for the PHP file tests/bootstrap.php defines a few options, and then also tells PHPUnit where the actual unit tests live Here files which are prefixed with “test-“ are only considered Note: only methods prefixed with “test” will be considered a unit test. All other methods will be skipped.
  • 27. Things to note down while writing tests Separate the tests into multiple files so that they are logically organized Every class contains an extension of the WP_UnitTestCase class. The name of the class does not really matter, just name it something that makes sense.
  • 28. Assertions An assertion is one “check” within the the test to determine if a value is equal to what we expect. Tests Group of one or more assertions are Tests
  • 29. One test with a single assertion: function test_ktm_message() { $string = ‘WordCamp Kathmandu 2017'; $this->assertEquals( 'WordCamp Kathmandu 2017', $string ); } One test with a two assertions: function test_ktm_message() { $string = ‘WordCamp Kathmandu 2017 - Awesome Event‘; $this->assertEquals( 'WordCamp Kathmandu 2017 - Awesome Event', $string ); $this->assertNotEquals( 'WordCamp Kathmandu 2017', $string ); } https://phpunit.de/manual/current/en/appendixes.assertions.html
  • 30. Q&A