Test Driven Development in Wordpress

Julio López Montalvo



What the fuck?

Manual Testing

  • var_dump()
  • print_r()
  • error_log()
  • debug_backtrace()

Test Driven Development

Write your tests, then write code to make your test pass and finally refactor your functional code

Unit Tests

  • Tests that verify that your code work as they were intended to.
  • Unit refer to units of code.
  • Think functions.


  • Detect problems early
  • Instant. Very fast feedback
  • Faster release cycles
  • Write modular code
  • Ensure quality over time



Dependency Manager for PHP


  • Install via composer:
    • $ composer create-project wp-cli/wp-cli --no-dev


A command line interface for WordPress

Create project Wordpress

$ wp core download

Create a Plugin

$ wp scaffold plugin demo

We need a test environment!

Automated Test Wordpress

  • Install subversion
  • svn co http://develop.svn.wordpress.org/trunk/
  • In your plugin: tests/bootstrap.php need look to trunk-folder/tests/phpunit/includes/functions.php


  • Install via composer
    • $ composer global require "phpunit/phpunit=4.1.*

  • For run your tests, in the root of the plugin run:
    • $ phpunit


Testing framework for PHP

setUp() && tearDown()

setUp() method is run just before each and every test method.

tearDown() is run just after each and every test method.

setUpBeforeClass() and tearDownAfterClass() are called before the first test and after the last test, respectively.


Pass Test


Failed asserting that an array contains 4


Failed asserting that 0 matches expected 1


  • assertArrayHasKey()
  • assertClassHasAttribute()
  • assertClassHasStaticAttribute()
  • assertContains()
  • assertContainsOnly()
  • assertContainsOnlyInstancesOf()
  • assertCount()
  • assertEmpty()
  • assertEqualXMLStructure()
  • assertEquals()
  • assertFalse()
  • assertFileEquals()
  • assertFileExists()
  • assertGreaterThan()
  • assertGreaterThanOrEqual()
  • assertInstanceOf()
  • assertInternalType()
  • assertJsonFileEqualsJsonFile()
  • assertJsonStringEqualsJsonFile()
  • assertJsonStringEqualsJsonString()
  • assertLessThan()
  • assertLessThanOrEqual()
  • assertNull()
  • assertObjectHasAttribute()
  • assertRegExp()
  • assertStringMatchesFormat()
  • assertStringMatchesFormatFile()
  • assertSame()
  • assertStringEndsWith()
  • assertStringEqualsFile()
  • assertStringStartsWith()
  • assertThat()
  • assertTrue()
  • assertXmlFileEqualsXmlFile()
  • assertXmlStringEqualsXmlFile()
  • assertXmlStringEqualsXmlString()

Writing Unit Tests

Let's write Factory Women

A Wordpress's plugin to setting up posts as test data


Failed assert: Undefined index: factory-women

Plugin code

Pass Test


Failed assert: call to undefined method insert_posts()

Plugin code

Pass Test


Failed asserting that 0 matches expected 5

Plugin code

Pass Test

Test code

Failed asserting that ' ' contains "prueba"

Plugin code

Pass Test

Now, everything that say my tests work =)

Thank you

  • Files: https://github.com/TheBlasfem/factory-womenBullet Two
  • Plugin's url: https://wordpress.org/plugins/factory-women/

Test Driven Development in Wordpress

By Julio López Montalvo

Test Driven Development in Wordpress

Learn how to write Unit Tests for your plugins in Wordpress.

  • 8,940
Loading comments...

More from Julio López Montalvo