Test Driven Development in Wordpress

Julio López Montalvo

@TheBlasfem

TDD

What the fuck?

Manual Testing

  • WP_DEBUG
  • 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.

Benefits

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

Composer

https://getcomposer.org/

Dependency Manager for PHP

Wp-cli

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

http://wp-cli.org/

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
http://make.wordpress.org/core/handbook/automated-testing/

Phpunit

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

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

http://phpunit.de/

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.

WP_UnitTestCase

Pass Test

assertContains()

Failed asserting that an array contains 4

assertEquals()

Failed asserting that 0 matches expected 1

Assertions

  • 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

Test

Failed assert: Undefined index: factory-women

Plugin code

Pass Test

Test

Failed assert: call to undefined method insert_posts()

Plugin code

Pass Test

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.

  • 10,402