Share
Embed
Unit Testing WordPress Plugins
Check out the video recording of this presentation:
http://www.youtube.com/watch?v=9qMUc9anJKQ
Bryan Petty
September 22, 2012
Transcript
23 slides
-
• 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? -
• Detect Problems Early • More Flexible Code • Faster
Release Cycles • Living Code Specifications • Backwards Compatibility The Benefits of Automated Testing -
• Built on PHPUnit • Separate Code Repository and Issue
Tracker ◦ http://unit-test.trac.wordpress.org/ ◦ http://unit-test.svn.wordpress.org/ • 1,366 Tests Currently • For the fans of git out there, you really should just use SVN here for now. • See the Core Contributor Handbook: ◦ make.wordpress.org/core/handbook/automated-testing Core Tests Architecture -
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 -
• 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 -
• 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 -
github.com/tierra/wordpress-plugin-tests WordPress Plugin Test Code • Copy bootstrap_tests. php, phpunit.xml,
and the tests folder to your plugin. • Add your main plugin file to bootstrap_tests.php. -
• Update the bootstrap_tests.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 -
$ cd wordpress-tests/wordpress/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! -
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 -
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 -
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/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? -
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 # Versions of PHP to test against php: - "5.2" - "5.3" - "5.4" # Versions of WordPress to test against # WP_VERSION = WordPress version number # WP_MULTISITE = Enable Multisite? env: - WP_VERSION=master WP_MULTISITE=0 - WP_VERSION=3.4.2 WP_MULTISITE=0 - WP_VERSION=3.3.3 WP_MULTISITE=0 - WP_VERSION=3.2.1 WP_MULTISITE=0