SlideShare a Scribd company logo
Unit testing for WordPress
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!
धन्यवाद :)

More Related Content

What's hot

Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-framework
Arulalan T
 
CI / CD w/ Codeception
CI / CD w/ CodeceptionCI / CD w/ Codeception
CI / CD w/ Codeception
Tudor Barbu
 
Functional Testing Swing Applications with Frankenstein
Functional Testing Swing Applications with FrankensteinFunctional Testing Swing Applications with Frankenstein
Functional Testing Swing Applications with Frankenstein
vivek_prahlad
 
Automation testing with Drupal 8
Automation testing with Drupal 8Automation testing with Drupal 8
Automation testing with Drupal 8
nagpalprachi
 
Unit testing
Unit testingUnit testing
Unit testing
Arthur Purnama
 
Pengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDDPengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDD
tlabamazing
 
Why Windows 8 drivers are buggy
Why Windows 8 drivers are buggyWhy Windows 8 drivers are buggy
Why Windows 8 drivers are buggy
PVS-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 Tutorial
Anup 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 2018
Testbytes
 
Test
TestTest
Test
Eddie Kao
 
Drupalcamp Simpletest
Drupalcamp SimpletestDrupalcamp Simpletest
Drupalcamp Simpletest
lyricnz
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails Apps
Rabble .
 
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
Sam Becker
 
Zend Framework 2 - PHPUnit
Zend Framework 2 - PHPUnitZend Framework 2 - PHPUnit
Zend Framework 2 - PHPUnit
Tarun Kumar Singhal
 
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
Mediacurrent
 
Automated Testing in Django
Automated Testing in DjangoAutomated Testing in Django
Automated Testing in Django
Loek van Gent
 
Drupal 7 ci and testing
Drupal 7 ci and testingDrupal 7 ci and testing
Drupal 7 ci and testing
Claudio Beatrice
 
JAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineJAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & Jasmine
Anup Singh
 
Selenium Handbook
Selenium HandbookSelenium Handbook
Selenium Handbook
Suresh Thammishetty
 
Front end unit testing using jasmine
Front end unit testing using jasmineFront end unit testing using jasmine
Front end unit testing using jasmine
Gil Fink
 

What's hot (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
 

Similar to 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
Tricode (part of Dept)
 
PHPUnit
PHPUnitPHPUnit
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
Mike Lively
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
Mindfire Solutions
 
Upstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testingUpstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testing
DanWooster1
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to hero
Jeremy Cook
 
Joomla! Testing - J!DD Germany 2016
Joomla! Testing - J!DD Germany 2016Joomla! Testing - J!DD Germany 2016
Joomla! Testing - J!DD Germany 2016
Yves Hoppe
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnit
James Fuller
 
Unit Testing in PHP
Unit Testing in PHPUnit Testing in PHP
Unit Testing in PHP
Radu Murzea
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And Drupal
Peter 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 PHPUnit
varuntaliyan
 
Phpunit
PhpunitPhpunit
Phpunit
japan_works
 
Php unit (eng)
Php unit (eng)Php unit (eng)
Php unit (eng)
Anatoliy Okhotnikov
 
Pragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScriptPragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScript
davejohnson
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Anand Kumar Rajana
 
Testing with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifeTesting with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs Life
Peter Gfader
 
Unit test
Unit testUnit test
Unit test
Tran Duc
 
What is selenium
What is seleniumWhat is selenium
What is selenium
Pesara Swamy
 
Python and test
Python and testPython and test
Python and test
Micron Technology
 

Similar to 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
 

Recently uploaded

Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...
Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...
Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...
OnePlan Solutions
 
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
shanihomely
 
React Native vs Flutter - SSTech System
React Native vs Flutter  - SSTech SystemReact Native vs Flutter  - SSTech System
React Native vs Flutter - SSTech System
SSTech System
 
🚂🚘 Premium Girls Call Ranchi 🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
🚂🚘 Premium Girls Call Ranchi  🛵🚡000XX00000 💃 Choose Best And Top Girl Service...🚂🚘 Premium Girls Call Ranchi  🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
🚂🚘 Premium Girls Call Ranchi 🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
bahubalikumar09988
 
Mobile App Development Company in Noida - Drona Infotech.
Mobile App Development Company in Noida - Drona Infotech.Mobile App Development Company in Noida - Drona Infotech.
Mobile App Development Company in Noida - Drona Infotech.
Mobile App Development Company in Noida - Drona Infotech
 
當測試開始左移
當測試開始左移當測試開始左移
當測試開始左移
Jersey (CHE-PING) Su
 
ERP Software Solutions Provider in Coimbatore
ERP Software Solutions Provider in CoimbatoreERP Software Solutions Provider in Coimbatore
ERP Software Solutions Provider in Coimbatore
Nextskill Technologies
 
AWS DevOps-Tutorial CHANAKYA SRIYAN DUKKA.
AWS DevOps-Tutorial CHANAKYA SRIYAN DUKKA.AWS DevOps-Tutorial CHANAKYA SRIYAN DUKKA.
AWS DevOps-Tutorial CHANAKYA SRIYAN DUKKA.
Srinivas Dukka
 
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
simran hot girls
 
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdfA Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
kalichargn70th171
 
AI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdf
AI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdfAI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdf
AI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdf
Daniel Zivkovic
 
Il Data Streaming per un’AI real-time di nuova generazione
Il Data Streaming per un’AI real-time di nuova generazioneIl Data Streaming per un’AI real-time di nuova generazione
Il Data Streaming per un’AI real-time di nuova generazione
confluent
 
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
revolutionary575
 
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
rachitkumar09887
 
HIRE A HACKER FOR CHEATING HUSBAND/WIFE)
HIRE A HACKER FOR CHEATING HUSBAND/WIFE)HIRE A HACKER FOR CHEATING HUSBAND/WIFE)
HIRE A HACKER FOR CHEATING HUSBAND/WIFE)
josephinedrea942
 
bangalore Girls call 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
bangalore Girls call  👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Deliverybangalore Girls call  👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
bangalore Girls call 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
sunilverma7884
 
Cisco Live Announcements: New ThousandEyes Release Highlights - July 2024
Cisco Live Announcements: New ThousandEyes Release Highlights - July 2024Cisco Live Announcements: New ThousandEyes Release Highlights - July 2024
Cisco Live Announcements: New ThousandEyes Release Highlights - July 2024
ThousandEyes
 
Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...
Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...
Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...
norina2645
 
Verified Girls Call Mumbai 👀 9820252231 👀 Cash Payment With Room DeliveryDeli...
Verified Girls Call Mumbai 👀 9820252231 👀 Cash Payment With Room DeliveryDeli...Verified Girls Call Mumbai 👀 9820252231 👀 Cash Payment With Room DeliveryDeli...
Verified Girls Call Mumbai 👀 9820252231 👀 Cash Payment With Room DeliveryDeli...
87tomato
 
TEQnation 2024: Sustainable Software: May the Green Code Be with You
TEQnation 2024: Sustainable Software: May the Green Code Be with YouTEQnation 2024: Sustainable Software: May the Green Code Be with You
TEQnation 2024: Sustainable Software: May the Green Code Be with You
marcofolio
 

Recently uploaded (20)

Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...
Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...
Maximizing Efficiency and Profitability: Optimizing Data Systems, Enhancing C...
 
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
Russian Girls Call Mumbai 🎈🔥9930687706 🔥💋🎈 Provide Best And Top Girl Service ...
 
React Native vs Flutter - SSTech System
React Native vs Flutter  - SSTech SystemReact Native vs Flutter  - SSTech System
React Native vs Flutter - SSTech System
 
🚂🚘 Premium Girls Call Ranchi 🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
🚂🚘 Premium Girls Call Ranchi  🛵🚡000XX00000 💃 Choose Best And Top Girl Service...🚂🚘 Premium Girls Call Ranchi  🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
🚂🚘 Premium Girls Call Ranchi 🛵🚡000XX00000 💃 Choose Best And Top Girl Service...
 
Mobile App Development Company in Noida - Drona Infotech.
Mobile App Development Company in Noida - Drona Infotech.Mobile App Development Company in Noida - Drona Infotech.
Mobile App Development Company in Noida - Drona Infotech.
 
當測試開始左移
當測試開始左移當測試開始左移
當測試開始左移
 
ERP Software Solutions Provider in Coimbatore
ERP Software Solutions Provider in CoimbatoreERP Software Solutions Provider in Coimbatore
ERP Software Solutions Provider in Coimbatore
 
AWS DevOps-Tutorial CHANAKYA SRIYAN DUKKA.
AWS DevOps-Tutorial CHANAKYA SRIYAN DUKKA.AWS DevOps-Tutorial CHANAKYA SRIYAN DUKKA.
AWS DevOps-Tutorial CHANAKYA SRIYAN DUKKA.
 
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
Girls Call Jogeshwari 9967584737 Provide Best And Top Girl Service And No1 in...
 
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdfA Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
A Step-by-Step Guide to Selecting the Right Automated Software Testing Tools.pdf
 
AI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdf
AI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdfAI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdf
AI - Your Startup Sidekick (Leveraging AI to Bootstrap a Lean Startup).pdf
 
Il Data Streaming per un’AI real-time di nuova generazione
Il Data Streaming per un’AI real-time di nuova generazioneIl Data Streaming per un’AI real-time di nuova generazione
Il Data Streaming per un’AI real-time di nuova generazione
 
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
Busty Girls Call Mumbai 9930245274 Unlimited Short Providing Girls Service Av...
 
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
Agra Girls Call Agra 0X0000000X Unlimited Short Providing Girls Service Avail...
 
HIRE A HACKER FOR CHEATING HUSBAND/WIFE)
HIRE A HACKER FOR CHEATING HUSBAND/WIFE)HIRE A HACKER FOR CHEATING HUSBAND/WIFE)
HIRE A HACKER FOR CHEATING HUSBAND/WIFE)
 
bangalore Girls call 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
bangalore Girls call  👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Deliverybangalore Girls call  👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
bangalore Girls call 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
 
Cisco Live Announcements: New ThousandEyes Release Highlights - July 2024
Cisco Live Announcements: New ThousandEyes Release Highlights - July 2024Cisco Live Announcements: New ThousandEyes Release Highlights - July 2024
Cisco Live Announcements: New ThousandEyes Release Highlights - July 2024
 
Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...
Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...
Celebrity Girls Call Mumbai 🛵🚡9910780858 💃 Choose Best And Top Girl Service A...
 
Verified Girls Call Mumbai 👀 9820252231 👀 Cash Payment With Room DeliveryDeli...
Verified Girls Call Mumbai 👀 9820252231 👀 Cash Payment With Room DeliveryDeli...Verified Girls Call Mumbai 👀 9820252231 👀 Cash Payment With Room DeliveryDeli...
Verified Girls Call Mumbai 👀 9820252231 👀 Cash Payment With Room DeliveryDeli...
 
TEQnation 2024: Sustainable Software: May the Green Code Be with You
TEQnation 2024: Sustainable Software: May the Green Code Be with YouTEQnation 2024: Sustainable Software: May the Green Code Be with You
TEQnation 2024: Sustainable Software: May the Green Code Be with You
 

Unit testing for WordPress

  • 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