Slide 1

Slide 1 text

Unit Testing WordPress Plugins Bryan Petty [email protected] github.com/tierra @ibakunet

Slide 2

Slide 2 text

● Unit tests are tests that verify that your smallest pieces of code ("units") in your plugin work as they were intended to. ● Unit tests are fast. ● Unit tests are reliable. ● Unit tests are flexible. What are Unit Tests?

Slide 3

Slide 3 text

● Detect Problems Early ● More Flexible Code ● Faster Release Cycles ● Living Code Specifications ● Backwards Compatibility The Benefits of Automated Testing

Slide 4

Slide 4 text

WordPress Core Unit Testing Framework

Slide 5

Slide 5 text

● Built on PHPUnit ● Develop Code Repository ○ http://develop.svn.wordpress.org/ ● 1,918 Tests Currently ● See the Core Contributor Handbook: ○ make.wordpress.org/core/handbook/automated-testing Core Tests Architecture

Slide 6

Slide 6 text

class Tests_Tests extends WP_UnitTestCase { function test_tests() { $this->assertTrue( true ); } } ● MySQL Transactions ● Automatically Flushes the Object Cache ● Clears Form Data ● Skips Tests Marked with Open Tickets WP_UnitTestCase

Slide 7

Slide 7 text

● Quickly create test posts, comments, users, terms, categories, and even entire blogs. $this->factory->post->create_many( 25 ); $this->factory->post->create( array( 'post_type' => 'page' ) ); $this->factory->user->create( array( 'role' => 'author' ) ); $this->factory->blog->create_many( 5 ); $this->factory->term->create( array( 'taxonomy' => 'category' ) ); Another Day at the Factory

Slide 8

Slide 8 text

● Fake AJAX requests without performing an external HTTP request. $_POST['_ajax_nonce'] = wp_create_nonce( 'get-comments' ); $_POST['action'] = 'get-comments'; $_POST['p'] = $this->_comment_post->ID; // Make the request try { $this->_handleAjax( 'get-comments' ); } catch ( WPAjaxDieContinueException $e ) { unset( $e ); } // Get the response $response = $this->_last_response; WP_Ajax_UnitTestCase

Slide 9

Slide 9 text

Testing Your Plugin

Slide 10

Slide 10 text

github.com/tierra/wordpress-plugin-tests WordPress Plugin Test Code ● Copy .travis.yml, phpunit. xml.dist, and the tests directory into the root folder of your plugin. ● Open tests/bootstrap.php and update the active_plugins setting to point to your main plugin file.

Slide 11

Slide 11 text

● Update the bootstrap.php file with the location of your plugin. $GLOBALS['wp_tests_options'] = array( 'active_plugins' => array( 'your-plugin-slug/your-main-plugin-file.php' ), ); ● Example Plugin: WP Slider Captcha ○ github.com/tierra/WP-Slider-Captcha Setup Your Plugin with WP Tests

Slide 12

Slide 12 text

$ cd wordpress/src/wp-content/plugins/WP-Slider-Captcha $ phpunit Running as single site... To run multisite, use -c multisite.xml Not running ajax tests... To execute these, use --group ajax. PHPUnit 3.6.11 by Sebastian Bergmann. Configuration read from phpunit.xml .S. Time: 1 second, Memory: 41.00Mb OK, but incomplete or skipped tests! Tests: 3, Assertions: 2, Skipped: 1. Run the Tests!

Slide 13

Slide 13 text

Writing Unit Tests

Slide 14

Slide 14 text

class WP_Test_WP_Slider_Captcha extends WP_UnitTestCase { function test_init_hook_was_added() { $this->assertGreaterThan( 0, has_filter( 'init', 'wpsc_scripts' ) ); } } test_wp_slider_captcha.php

Slide 15

Slide 15 text

function test_threshold_sanitize_less_than_one() { $this->assertEquals( 60, wpsc_threshold_sanitize( 0 ) ); } function test_threshold_sanitize_more_than_100() { $this->assertEquals( 60, wpsc_threshold_sanitize( 101 ) ); } function test_threshold_sanitize_non_number() { $this->assertEquals( 0, wpsc_threshold_sanitize( 'I am a string' ) ); } function test_threshold_sanitize_valid_number() { $this->assertEquals( 40, wpsc_threshold_sanitize( 40 ) ); } test_wp_slider_captcha.php

Slide 16

Slide 16 text

Covering All Your Bases

Slide 17

Slide 17 text

The WordPress core unit tests can be easily configured to run with your plugin activated by adding this code to the beginning of wordpress/tests/phpunit/includes/bootstrap.php: $GLOBALS['wp_tests_options'] = array( 'active_plugins' => array( "WP_Slider_Captcha/wp-slider-captcha.php" ), ); Does Your Plugin Break Anything in WordPress Itself?

Slide 18

Slide 18 text

Configure Travis CI for Truly Automated Tests

Slide 19

Slide 19 text

Copy .travis.yml to the root of your plugin, activate Travis CI with your GitHub repo, then push the changes to your plugin on GitHub. Travis CI Settings # Tell Travis CI we're using PHP language: php php: - "5.5" env: - WP_VERSION=master matrix: include: - php: "5.3" env: WP_VERSION=master - php: "5.4" env: WP_VERSION=3.8 - php: "5.2" env: WP_VERSION=3.8

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

wordpress.tv/speakers/aaron-jorbin Write Javascript Unit Tests with QUnit

Slide 22

Slide 22 text

wordpress.tv/speakers/nikolay-bachiyski Another Unit Testing Example

Slide 23

Slide 23 text

Questions speakerdeck.com/u/tierra Bryan Petty [email protected] github.com/tierra @ibakunet