SlideShare une entreprise Scribd logo
1  sur  24
Télécharger pour lire hors ligne
UNIT TESTING
#wpm013
BARRY KOOIJ
Senior Web Developer @ Yoast
Plugin developer
What The File
Sub Posts
Contributor EDD
EDD extensies
Moderator WPNL forum
Twitter: @cageNL
MIJN SETUP
Device
IDE
Versiebeheer
ENV

MacBook Pro
PhpStorm
GIT, GitHub
Vagrant w/ VVV
Varying Vagrant Vagrants
WHAT
WHAT ARE UNIT TESTS
“An automated piece of code that invokes your application
code to check a single assumption.” — Pirate Dunbar
MANUAL TESTING
Very time consuming
High chance on errors
Single point of knowledge
UNIT TESTING
Instant / very fast feedback
Low chance on errors
Knowledge put in code
HOW
DEBUGGING
define( 'WP_DEBUG', true );
!
if ( WP_DEBUG ) {
define( 'SCRIPT_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', true );
@ini_set( 'display_errors', 1 );
}
!
display_errors = On;
error_reporting = E_ALL | E_STRICT;
TWO WAYS OF UNIT TESTING
Write your tests, then write code to make your tests pass
Write your code, then write tests that pass
PHPUNIT
A command line tool that runs unit tests & reports their
results.
Created by Sebastian Bergmann
Integrated in most IDE (Eclipse, Netbeans, PHPStorm)
Auto installed in Vagrant w/ Varying Vagrant Vagrants
UNIT TESTS REQUIREMENTS
Each test should be able to run on its own (isolated)
Easy to read
Quick to execute
Self explaining function names
Test classes should inherit from WP_UnitTestCase (which
inherits from PHPUnit_Framework_TestCase)
Test functions should start w/ test (test_my_test_name)
ARRANGE ACT ASSERT
Setup the context, all variables etc.
!
Call the method, do the action, query the database, etc.
!
Check if the result matches the expectations.
ARRANGE ACT ASSERT
function test_get_single_role_by_user_query() {
// Arrange
$this->factory->user->create_many( 2, array(
'role' => 'subscriber'
) );
!
!
!
!
!
!



}
ARRANGE ACT ASSERT
function test_get_single_role_by_user_query() {
// Arrange
$this->factory->user->create_many( 2, array(
'role' => 'subscriber'
) );
!
// Act
$wp_user_search = new
WP_User_Query( array( 'role' => 'subscriber' ) );
$users = $wp_user_search->get_results();
!
!
!
}
ARRANGE ACT ASSERT
function test_get_single_role_by_user_query() {
// Arrange
$this->factory->user->create_many( 2, array(
'role' => 'subscriber'
) );
!
// Act
$wp_user_search = new
WP_User_Query( array( 'role' => 'subscriber' ) );
$users = $wp_user_search->get_results();
!
// Assert
$this->assertEquals( 2, count( $users ) );
}
ASSERTION
A way of explicitly checking the assumptions that your
code makes.
!
assertEqual()
assertTrue()
assertNotNull()
assertContains()
assertGreaterThan()
!
http://phpunit.de/manual/3.8/en/writing-tests-forphpunit.html#writing-tests-for-phpunit.assertions
EXAMPLE
Q&A
Twitter @cageNL
WordPress & Github: barrykooij
10 & 11 mei WordCamp NL

Contenu connexe

Tendances

Tendances (20)

North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
 
Night Watch with QA
Night Watch with QANight Watch with QA
Night Watch with QA
 
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
 
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
 
Testing nightwatch, by David Torroija
Testing nightwatch, by David TorroijaTesting nightwatch, by David Torroija
Testing nightwatch, by David Torroija
 
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 Frontend Testing
Automated Frontend TestingAutomated Frontend Testing
Automated Frontend Testing
 
Automate Thyself
Automate ThyselfAutomate Thyself
Automate Thyself
 
Test Driven Development Methodology and Philosophy
Test Driven Development Methodology and Philosophy Test Driven Development Methodology and Philosophy
Test Driven Development Methodology and Philosophy
 
Testing Web Applications
Testing Web ApplicationsTesting Web Applications
Testing Web Applications
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails Apps
 
Creating Gradle Plugins
Creating Gradle PluginsCreating Gradle Plugins
Creating Gradle Plugins
 
Static Code Analysis
Static Code AnalysisStatic Code Analysis
Static Code Analysis
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Create, test, secure, repeat
Create, test, secure, repeatCreate, test, secure, repeat
Create, test, secure, repeat
 
Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013
 
Introduction to Griffon
Introduction to GriffonIntroduction to Griffon
Introduction to Griffon
 
DDD with Behat
DDD with BehatDDD with Behat
DDD with Behat
 
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
 
TDD & BDD
TDD & BDDTDD & BDD
TDD & BDD
 

En vedette (6)

Related Content
Related ContentRelated Content
Related Content
 
Plugin development wpmeetup010
Plugin development wpmeetup010Plugin development wpmeetup010
Plugin development wpmeetup010
 
CMAT 101 Clubs
CMAT 101 ClubsCMAT 101 Clubs
CMAT 101 Clubs
 
Cmat 101 final project
Cmat 101 final projectCmat 101 final project
Cmat 101 final project
 
Logosweldproducts
LogosweldproductsLogosweldproducts
Logosweldproducts
 
Cmat 101 final project
Cmat 101 final projectCmat 101 final project
Cmat 101 final project
 

Similaire à Unit testing @ WordPress Meetup Tilburg 7 januari 2014

Quality Assurance for PHP projects - ZendCon 2012
Quality Assurance for PHP projects - ZendCon 2012Quality Assurance for PHP projects - ZendCon 2012
Quality Assurance for PHP projects - ZendCon 2012
Michelangelo van Dam
 
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
Enterprise PHP Center
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiIntro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi
 

Similaire à Unit testing @ WordPress Meetup Tilburg 7 januari 2014 (20)

Testing with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifeTesting with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs Life
 
Unit testing for WordPress
Unit testing for WordPressUnit testing for WordPress
Unit testing for WordPress
 
Php tests tips
Php tests tipsPhp tests tips
Php tests tips
 
Javascript tdd byandreapaciolla
Javascript tdd byandreapaciollaJavascript tdd byandreapaciolla
Javascript tdd byandreapaciolla
 
Better Testing With PHP Unit
Better Testing With PHP UnitBetter Testing With PHP Unit
Better Testing With PHP Unit
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Unit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSUnit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJS
 
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
 
Unit testing
Unit testingUnit testing
Unit testing
 
Quality Assurance for PHP projects - ZendCon 2012
Quality Assurance for PHP projects - ZendCon 2012Quality Assurance for PHP projects - ZendCon 2012
Quality Assurance for PHP projects - ZendCon 2012
 
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
 
UI Testing
UI TestingUI Testing
UI Testing
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnit
 
Pragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScriptPragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScript
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiIntro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran Mizrahi
 
Junit_.pptx
Junit_.pptxJunit_.pptx
Junit_.pptx
 
Codeception presentation
Codeception presentationCodeception presentation
Codeception presentation
 
Testing w-mocks
Testing w-mocksTesting w-mocks
Testing w-mocks
 
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
 
Watir Presentation Sumanth Krishna. A
Watir Presentation   Sumanth Krishna. AWatir Presentation   Sumanth Krishna. A
Watir Presentation Sumanth Krishna. A
 

Plus de Barry Kooij (7)

Plugin Development - WP Meetup Antwerp
Plugin Development - WP Meetup AntwerpPlugin Development - WP Meetup Antwerp
Plugin Development - WP Meetup Antwerp
 
Unit Testing in WordPress
Unit Testing in WordPressUnit Testing in WordPress
Unit Testing in WordPress
 
We Will VAT You
We Will VAT YouWe Will VAT You
We Will VAT You
 
Customizing Your WooCommerce Store
Customizing Your WooCommerce StoreCustomizing Your WooCommerce Store
Customizing Your WooCommerce Store
 
Automating your releases with shell scripts - WordCamp Netherlands 2014
Automating your releases with shell scripts - WordCamp Netherlands 2014Automating your releases with shell scripts - WordCamp Netherlands 2014
Automating your releases with shell scripts - WordCamp Netherlands 2014
 
Plugin Development @ WordCamp Norway 2014
Plugin Development @ WordCamp Norway 2014Plugin Development @ WordCamp Norway 2014
Plugin Development @ WordCamp Norway 2014
 
WordPress pizza sessie
WordPress pizza sessieWordPress pizza sessie
WordPress pizza sessie
 

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Unit testing @ WordPress Meetup Tilburg 7 januari 2014

  • 2. BARRY KOOIJ Senior Web Developer @ Yoast Plugin developer What The File Sub Posts Contributor EDD EDD extensies Moderator WPNL forum Twitter: @cageNL
  • 3. MIJN SETUP Device IDE Versiebeheer ENV MacBook Pro PhpStorm GIT, GitHub Vagrant w/ VVV Varying Vagrant Vagrants
  • 4.
  • 6. WHAT ARE UNIT TESTS “An automated piece of code that invokes your application code to check a single assumption.” — Pirate Dunbar
  • 7. MANUAL TESTING Very time consuming High chance on errors Single point of knowledge
  • 8.
  • 9. UNIT TESTING Instant / very fast feedback Low chance on errors Knowledge put in code
  • 10.
  • 11. HOW
  • 12. DEBUGGING define( 'WP_DEBUG', true ); ! if ( WP_DEBUG ) { define( 'SCRIPT_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', true ); @ini_set( 'display_errors', 1 ); } ! display_errors = On; error_reporting = E_ALL | E_STRICT;
  • 13. TWO WAYS OF UNIT TESTING Write your tests, then write code to make your tests pass Write your code, then write tests that pass
  • 14. PHPUNIT A command line tool that runs unit tests & reports their results. Created by Sebastian Bergmann Integrated in most IDE (Eclipse, Netbeans, PHPStorm) Auto installed in Vagrant w/ Varying Vagrant Vagrants
  • 15. UNIT TESTS REQUIREMENTS Each test should be able to run on its own (isolated) Easy to read Quick to execute Self explaining function names Test classes should inherit from WP_UnitTestCase (which inherits from PHPUnit_Framework_TestCase) Test functions should start w/ test (test_my_test_name)
  • 16. ARRANGE ACT ASSERT Setup the context, all variables etc. ! Call the method, do the action, query the database, etc. ! Check if the result matches the expectations.
  • 17. ARRANGE ACT ASSERT function test_get_single_role_by_user_query() { // Arrange $this->factory->user->create_many( 2, array( 'role' => 'subscriber' ) ); ! ! ! ! ! ! 
 }
  • 18. ARRANGE ACT ASSERT function test_get_single_role_by_user_query() { // Arrange $this->factory->user->create_many( 2, array( 'role' => 'subscriber' ) ); ! // Act $wp_user_search = new WP_User_Query( array( 'role' => 'subscriber' ) ); $users = $wp_user_search->get_results(); ! ! ! }
  • 19. ARRANGE ACT ASSERT function test_get_single_role_by_user_query() { // Arrange $this->factory->user->create_many( 2, array( 'role' => 'subscriber' ) ); ! // Act $wp_user_search = new WP_User_Query( array( 'role' => 'subscriber' ) ); $users = $wp_user_search->get_results(); ! // Assert $this->assertEquals( 2, count( $users ) ); }
  • 20. ASSERTION A way of explicitly checking the assumptions that your code makes. ! assertEqual() assertTrue() assertNotNull() assertContains() assertGreaterThan() ! http://phpunit.de/manual/3.8/en/writing-tests-forphpunit.html#writing-tests-for-phpunit.assertions
  • 21.
  • 22.
  • 24. Q&A Twitter @cageNL WordPress & Github: barrykooij 10 & 11 mei WordCamp NL