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
Unit testing @ WordPress Meetup Tilburg 7 januari 2014
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 @ WordPress Meetup Tilburg 7 januari 2014
UNIT TESTING
Instant / very fast feedback
Low chance on errors
Knowledge put in code
Unit testing @ WordPress Meetup Tilburg 7 januari 2014
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
Unit testing @ WordPress Meetup Tilburg 7 januari 2014
Unit testing @ WordPress Meetup Tilburg 7 januari 2014
EXAMPLE
Q&A
Twitter @cageNL
WordPress & Github: barrykooij
10 & 11 mei WordCamp NL

More Related Content

PDF
How Testing Changed My Life
PPTX
Unit testing plugins: The 5 W's and an H
PDF
Automated Testing in WordPress, Really?!
PDF
Continuous Integration Testing in Django
PPTX
Automation testing with Drupal 8
PDF
cf.Objective() 2017 - Design patterns - Brad Wood
PDF
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
PDF
Efficient JavaScript Unit Testing, May 2012
How Testing Changed My Life
Unit testing plugins: The 5 W's and an H
Automated Testing in WordPress, Really?!
Continuous Integration Testing in Django
Automation testing with Drupal 8
cf.Objective() 2017 - Design patterns - Brad Wood
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
Efficient JavaScript Unit Testing, May 2012

What's hot (20)

PDF
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
PDF
Night Watch with QA
PDF
Test all the things! Automated testing with Drupal 8
PDF
3 WAYS TO TEST YOUR COLDFUSION API
PDF
Testing nightwatch, by David Torroija
ZIP
Five Easy Ways to QA Your Drupal Site
ZIP
Automated Frontend Testing
PDF
Automate Thyself
PDF
Test Driven Development Methodology and Philosophy
PDF
Testing Web Applications
PDF
Testing Legacy Rails Apps
PDF
Creating Gradle Plugins
PDF
Static Code Analysis
PDF
Living With Legacy Code
PDF
Create, test, secure, repeat
PDF
Rspec and Capybara Intro Tutorial at RailsConf 2013
PDF
Introduction to Griffon
PDF
DDD with Behat
PDF
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
PPTX
TDD & BDD
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
Night Watch with QA
Test all the things! Automated testing with Drupal 8
3 WAYS TO TEST YOUR COLDFUSION API
Testing nightwatch, by David Torroija
Five Easy Ways to QA Your Drupal Site
Automated Frontend Testing
Automate Thyself
Test Driven Development Methodology and Philosophy
Testing Web Applications
Testing Legacy Rails Apps
Creating Gradle Plugins
Static Code Analysis
Living With Legacy Code
Create, test, secure, repeat
Rspec and Capybara Intro Tutorial at RailsConf 2013
Introduction to Griffon
DDD with Behat
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
TDD & BDD
Ad

Viewers also liked (6)

PDF
Related Content
PPTX
Plugin development wpmeetup010
PPTX
CMAT 101 Clubs
PPTX
Cmat 101 final project
PDF
PPTX
Cmat 101 final project
Related Content
Plugin development wpmeetup010
CMAT 101 Clubs
Cmat 101 final project
Cmat 101 final project
Ad

Similar to Unit testing @ WordPress Meetup Tilburg 7 januari 2014 (20)

PDF
Unit testing for WordPress
PDF
Effizientere WordPress-Plugin-Entwicklung mit Softwaretests
PDF
Unit Testing in WordPress
PPTX
Getting started with PHPUnit
PPT
Automated Unit Testing
PPT
Unit testing php-unit - phing - selenium_v2
PPTX
Unit Testng with PHP Unit - A Step by Step Training
PPTX
PHPUnit: from zero to hero
PDF
Cursus phpunit
PDF
Intro to PHP Testing
PPTX
Introduction to Continous Integration with WordPress
PPT
Unit Testing using PHPUnit
PDF
Unit testing for WordPress
KEY
Developer testing 101: Become a Testing Fanatic
PDF
Fighting Fear-Driven-Development With PHPUnit
KEY
PHPUnit testing to Zend_Test
PPT
Test Driven Development with PHPUnit
PDF
Introduction to Unit Testing with PHPUnit
PDF
Automatic testing and quality assurance for WordPress plugins and themes
PDF
PhpUnit Best Practices
Unit testing for WordPress
Effizientere WordPress-Plugin-Entwicklung mit Softwaretests
Unit Testing in WordPress
Getting started with PHPUnit
Automated Unit Testing
Unit testing php-unit - phing - selenium_v2
Unit Testng with PHP Unit - A Step by Step Training
PHPUnit: from zero to hero
Cursus phpunit
Intro to PHP Testing
Introduction to Continous Integration with WordPress
Unit Testing using PHPUnit
Unit testing for WordPress
Developer testing 101: Become a Testing Fanatic
Fighting Fear-Driven-Development With PHPUnit
PHPUnit testing to Zend_Test
Test Driven Development with PHPUnit
Introduction to Unit Testing with PHPUnit
Automatic testing and quality assurance for WordPress plugins and themes
PhpUnit Best Practices

More from Barry Kooij (6)

PDF
Plugin Development - WP Meetup Antwerp
PDF
We Will VAT You
PDF
Customizing Your WooCommerce Store
PDF
Automating your releases with shell scripts - WordCamp Netherlands 2014
PDF
Plugin Development @ WordCamp Norway 2014
PPTX
WordPress pizza sessie
Plugin Development - WP Meetup Antwerp
We Will VAT You
Customizing Your WooCommerce Store
Automating your releases with shell scripts - WordCamp Netherlands 2014
Plugin Development @ WordCamp Norway 2014
WordPress pizza sessie

Recently uploaded (20)

PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPTX
Chapter 5: Probability Theory and Statistics
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PPTX
Modernising the Digital Integration Hub
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PPTX
Web Crawler for Trend Tracking Gen Z Insights.pptx
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
A novel scalable deep ensemble learning framework for big data classification...
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
sustainability-14-14877-v2.pddhzftheheeeee
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
A review of recent deep learning applications in wood surface defect identifi...
A comparative study of natural language inference in Swahili using monolingua...
Chapter 5: Probability Theory and Statistics
O2C Customer Invoices to Receipt V15A.pptx
Benefits of Physical activity for teenagers.pptx
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
Modernising the Digital Integration Hub
1 - Historical Antecedents, Social Consideration.pdf
Enhancing emotion recognition model for a student engagement use case through...
Univ-Connecticut-ChatGPT-Presentaion.pdf
NewMind AI Weekly Chronicles – August ’25 Week III
Web Crawler for Trend Tracking Gen Z Insights.pptx
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Hindi spoken digit analysis for native and non-native speakers
A novel scalable deep ensemble learning framework for big data classification...
Final SEM Unit 1 for mit wpu at pune .pptx
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Zenith AI: Advanced Artificial Intelligence
sustainability-14-14877-v2.pddhzftheheeeee

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
  • 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
  • 9. UNIT TESTING Instant / very fast feedback Low chance on errors Knowledge put in code
  • 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
  • 24. Q&A Twitter @cageNL WordPress & Github: barrykooij 10 & 11 mei WordCamp NL