SlideShare a Scribd company logo
Unit Testing in WordPress
WordSesh 3
Manual Testing
Time consuming
Error-prone
Single point of knowledge
Unit Testing in WordPress
Unit Testing
Time saving
Infallible
Knowledge in code
“A unit test is an automated piece of code that
invokes a unit of work in the system and then
checks a single assumption about the behavior
of that unit of work.”
Write code before tests
Write tests before code
Unit Testing in WordPress
PHPUnit
CLI tool that runs unit tests & reports their results.
Created by Sebastian Bergmann
Easy to install
Unit Tests Requirements
Each test should be able to run on its own (isolated)
Easy to read
Quick to execute
Self explaining function names
Unit Tests Requirements
Test classes should inherit from WP_UnitTestCase
(which inherits from PHPUnit_Framework_TestCase)
Test functions should start w/ test
(test_my_test_name)
–William C. Wake
“This test case fits a pattern I call ‘Arrange,
Act, Assert’: it sets up data, calls a method
under test, and then verifies that it worked as
expected.”
Arrange Act Assert
Easy to
- Read

- Follow

- Understand

- Maintain

Arrange
Arrange all necessary preconditions and inputs.
Act
Act on the object or method under test.
Assert
Assert that the expected results have occurred.
Assertion
assertEqual()

assertTrue()

assertNotNull()

assertContains()

assertGreaterThan()
https://phpunit.de/manual/3.7/en/writing-tests-for-
phpunit.html#writing-tests-for-phpunit.assertions
function test_get_single_role_by_user_query() {



// Arrange

$this->factory->user->create_many( 2, 

array( 'role' => 'subscriber' ) );



// Act 

$u_query = new WP_User_Query( array( 'role' => 'subscriber' ) );



// Assert

$this->assertEquals( 2, count( $u_query->get_results() ) );

}
function test_get_single_role_by_user_query() {



// Arrange

$this->factory->user->create_many( 2, 

array( 'role' => 'subscriber' ) );



// Act 

$u_query = new WP_User_Query( array( 'role' => 'subscriber' ) );



// Assert

$this->assertEquals( 2, count( $u_query->get_results() ) );

}
function test_get_single_role_by_user_query() {



// Arrange

$this->factory->user->create_many( 2, 

array( 'role' => 'subscriber' ) );



// Act 

$u_query = new WP_User_Query( array( 'role' => 'subscriber' ) );



// Assert

$this->assertEquals( 2, count( $u_query->get_results() ) );

}
function test_get_single_role_by_user_query() {



// Arrange

$this->factory->user->create_many( 2, 

array( 'role' => 'subscriber' ) );



// Act 

$u_query = new WP_User_Query( array( 'role' => 'subscriber' ) );



// Assert

$this->assertEquals( 2, count( $u_query->get_results() ) );

}
Unit Testing in WordPress
Unit Testing in WordPress
Tools
Unit Testing in WordPress
Unit Testing in WordPress
Unit Testing in WordPress
Unit Testing in WordPress
Unit Testing in WordPress
Unit Testing in WordPress
Unit Testing in WordPress
Barry Kooij
@CageNL
barrykooij.com

More Related Content

What's hot (20)

PDF
TestNG Annotations in Selenium | Edureka
Edureka!
 
PPTX
Unit testing and mocking in Python - PyCon 2018 - Kenya
Erick M'bwana
 
PDF
How to Record Scripts in JMeter? JMeter Script Recording Tutorial | Edureka
Edureka!
 
PPT
How to test models using php unit testing framework?
satejsahu
 
PPTX
Unit & integration testing
Pavlo Hodysh
 
PDF
Vladyslav Romanchenko "How to keep high code quality without e2e tests"
Dakiry
 
PDF
Angular Unit Testing NDC Minn 2018
Justin James
 
PPTX
How to make your functional tests really quick
Mikalai Alimenkou
 
ODP
Rails Testing
mikeblake
 
PPTX
Microsoft Fakes, Unit Testing the (almost) Untestable Code
Aleksandar Bozinovski
 
PPT
Test Driven Development using QUnit
satejsahu
 
PPT
Integration testing
Tsegabrehan Am
 
PDF
Php tests tips
Damian Sromek
 
PPTX
Automated testing of ASP .Net Core applications
nispas
 
PDF
Cypress - Best Practices
Brian Mann
 
PDF
Manage Selenium Grid with Terraform by Omkar Khatavkar (https://www.linkedin....
Software Testing Board
 
PPTX
Automated tests
Damian Sromek
 
PPTX
TestNG with selenium
Gousalya Ramachandran
 
PPT
Bealls florida automation overview
Vijay Rangaiah
 
PPTX
Selenium TestNG
KadarkaraiSelvam
 
TestNG Annotations in Selenium | Edureka
Edureka!
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Erick M'bwana
 
How to Record Scripts in JMeter? JMeter Script Recording Tutorial | Edureka
Edureka!
 
How to test models using php unit testing framework?
satejsahu
 
Unit & integration testing
Pavlo Hodysh
 
Vladyslav Romanchenko "How to keep high code quality without e2e tests"
Dakiry
 
Angular Unit Testing NDC Minn 2018
Justin James
 
How to make your functional tests really quick
Mikalai Alimenkou
 
Rails Testing
mikeblake
 
Microsoft Fakes, Unit Testing the (almost) Untestable Code
Aleksandar Bozinovski
 
Test Driven Development using QUnit
satejsahu
 
Integration testing
Tsegabrehan Am
 
Php tests tips
Damian Sromek
 
Automated testing of ASP .Net Core applications
nispas
 
Cypress - Best Practices
Brian Mann
 
Manage Selenium Grid with Terraform by Omkar Khatavkar (https://www.linkedin....
Software Testing Board
 
Automated tests
Damian Sromek
 
TestNG with selenium
Gousalya Ramachandran
 
Bealls florida automation overview
Vijay Rangaiah
 
Selenium TestNG
KadarkaraiSelvam
 

Similar to Unit Testing in WordPress (20)

PDF
PChomePay unit test basic
Eric Huang
 
PDF
Unit testing @ WordPress Meetup Tilburg 7 januari 2014
Barry Kooij
 
PDF
Unit testing for WordPress
Harshad Mane
 
PDF
Unit testing in PHP
Chonlasith Jucksriporn
 
PDF
Back to basics - PHPUnit
Sebastian Marek
 
PDF
Systematic Unit Testing
scotchfield
 
PPTX
Unit Testng with PHP Unit - A Step by Step Training
Ram Awadh Prasad, PMP
 
PPT
Test Driven Development with PHPUnit
Mindfire Solutions
 
PPT
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
PDF
Unit testing with PHPUnit - there's life outside of TDD
Paweł Michalik
 
PPTX
Unit testing plugins: The 5 W's and an H
Tom Jenkins
 
PPTX
PHPUnit: from zero to hero
Jeremy Cook
 
PPTX
Test in action – week 1
Yi-Huan Chan
 
PPTX
Introduction to Continous Integration with WordPress
Seagyn Davis
 
PPT
Automated Unit Testing
Mike Lively
 
PPTX
Unit testing for project managers
Zend by Rogue Wave Software
 
PPTX
Getting started with PHPUnit
Khyati Gala
 
KEY
Unit Testing Your Application
Paladin Web Services
 
PDF
PHPUnit with Magento
Tu Hoang
 
PChomePay unit test basic
Eric Huang
 
Unit testing @ WordPress Meetup Tilburg 7 januari 2014
Barry Kooij
 
Unit testing for WordPress
Harshad Mane
 
Unit testing in PHP
Chonlasith Jucksriporn
 
Back to basics - PHPUnit
Sebastian Marek
 
Systematic Unit Testing
scotchfield
 
Unit Testng with PHP Unit - A Step by Step Training
Ram Awadh Prasad, PMP
 
Test Driven Development with PHPUnit
Mindfire Solutions
 
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
Unit testing with PHPUnit - there's life outside of TDD
Paweł Michalik
 
Unit testing plugins: The 5 W's and an H
Tom Jenkins
 
PHPUnit: from zero to hero
Jeremy Cook
 
Test in action – week 1
Yi-Huan Chan
 
Introduction to Continous Integration with WordPress
Seagyn Davis
 
Automated Unit Testing
Mike Lively
 
Unit testing for project managers
Zend by Rogue Wave Software
 
Getting started with PHPUnit
Khyati Gala
 
Unit Testing Your Application
Paladin Web Services
 
PHPUnit with Magento
Tu Hoang
 
Ad

More from Barry Kooij (8)

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

Recently uploaded (20)

PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 

Unit Testing in WordPress