Unit Testing WordPress Plugins

Is your plugin broken?

Ben Lobaugh / ben.lobaugh.net / @benlobaugh

What is a Ben Lobaugh

Talk about Myself

Meetup/WordCamp Organizer

1300+ Members and growing!

Presentation Flow

  • Testing Concepts
  • Types of testing
  • Basic unit test
  • Getting unit tests running in WordPress

What is Automated Testing?

Method to ensure your program is performing to specifications

* 20k foot view

Why Test?

  • Prevents breakage on future releases
  • Saves time as tests are automated
  • You will NEVER forget that one test again
  • Creates a form of documentation on expectations of the code

What is Testing Not

  • True quality assurance
  • Provides a false sense of security
  • Only verifies what has been tested

Testing Concepts

Overview of several methods and when/why to use them.

*Unit Testing is commonly used to refer to all testing. It is actually one type of testing.

Acceptance Testing

A test conducted to determine if the requirements of a specification or contract are met.

* Also known as: Verification, Validation, Functional

Integration Testing

Tests have various components work together, both internally and with external APIs

Unit Testing

Testing as the single unit level, such as a single funtion. Often unit tests will include some form of Integration tests out of necessity.

Test Driven Development ( TDD )

With TDD the most minimal tests are written before and app code is created. Tests should fail. Make them pass and write another failing test.


Writing tests is complex. Funtions with many conditionals or multiple uses are hard to test. Testing helps find these to make your code more streamlined, compact, and less likely to break due to it doing too many things.

The Toolbox

Useful tools to carry on your journey


Assertions are used to verify the result of a function call is what is expected.

See the phpunit manual at phpunit.de for a complete list of assertions


Mocks make your app think it is using some object, commonly used for databases and API interactions.


Fixtures are sets of predefined data or settings that can be used across tests.

Code Coverage

Check to see how many lines of your app are covered with a unit test. Can travel through conditionals. Helps ensure your test hits all provisions of whatever it is testing.

The Test

Quick overview of the code used in a generic phpunit test.

class SomeTests extends PHPUnit_Framework_TestCase {
    public function test_returns_a_boolean() {
        $this->assertInternalType( 'boolean', funcDoesArray() );

class SomeTests extends

PHPUnit intelligently finds which classes in a file contain the tests. Also a lot of great functionality is provided in the parent class. See: phpunit.de/manual/

public function test_returns_a_boolean()

All tests begin with 'test_'. Any functions not starting with test_ will not be run by phpunit and should be helper functions.

$this->assertInternalType( 'boolean', funcDoesArray() );

Assert functions are the actual tests. They verify the expected results of a test. In general there should only be one assert.

The Test File

Test file names begin with 'test_'.

Running the Tests

Complex so pay attention!

  • Open Terminal
  • cd to project directory
  • Run 'phpunit'
  • If all success drink Scotch

Testing the WordPress Way

  • Protect your database!
  • Start with the WordPress core test setup

Get the WordPress code

					    svn co http://develop.svn.wordpress.org/trunk/

Get the Plugin Bits

					    git clone https://github.com/blobaugh/wordpress-plugin-tests

Setup the Bootstrap Details

In tests/bootstrap.php setup the folder and filename of your plugin

$GLOBALS['wp_tests_options'] = array(
	'active_plugins' => array( "PLUGIN_FOLDER/PLUGIN_FILE.php" ),

The WordPress Test Class

class WP_Test_WordPress_Plugin_Tests extends WP_UnitTestCase

Drink Scotch!

Bonus: Continuous Integration

Service that compiles your app and runs the tests for every commit automagically



Free, integrates with Github

& Resources

BY Ben Lobaugh / ben.lobaugh.net / @benlobaugh