Unit testing @ WordPress Meetup Tilburg 7 januari 2014

Post on 19-May-2015

471 views 0 download

Tags:

description

Unit testing @ WordPress Meetup Tilburg 7 januari 2014

Transcript of Unit testing @ WordPress Meetup Tilburg 7 januari 2014

UNIT TESTING

#wpm013

UNIT TESTING

BARRY KOOIJ

Senior Web Developer @ Yoast Plugin developer What The File Sub Posts Contributor EDD EDD extensies Moderator WPNL forum Twitter: @cageNL

MIJN SETUP

Device MacBook Pro IDE PhpStorm Versiebeheer GIT, GitHub ENV Vagrant w/ VVV Varying Vagrant Vagrants

WHAT

WHAT ARE UNIT TESTS

“An automated piece of code that invokes your application code to check a single assumption.” — Pirate Dunbar

MANUAL TESTING

Very time consuming High chance on errors Single point of knowledge

UNIT TESTING

Instant / very fast feedback Low chance on errors Knowledge put in code

HOW

DEBUGGING

define( 'WP_DEBUG', true ); !if ( WP_DEBUG ) { define( 'SCRIPT_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', true ); @ini_set( 'display_errors', 1 ); } !display_errors = On; error_reporting = E_ALL | E_STRICT;

TWO WAYS OF UNIT TESTING

Write your tests, then write code to make your tests pass Write your code, then write tests that pass

PHPUNIT

A command line tool that runs unit tests & reports their results. Created by Sebastian Bergmann Integrated in most IDE (Eclipse, Netbeans, PHPStorm) Auto installed in Vagrant w/ Varying Vagrant Vagrants

UNIT TESTS REQUIREMENTS

Each test should be able to run on its own (isolated) Easy to read Quick to execute Self explaining function names Test classes should inherit from WP_UnitTestCase (which inherits from PHPUnit_Framework_TestCase) Test functions should start w/ test (test_my_test_name)

ARRANGE ACT ASSERT

Setup the context, all variables etc. !Call the method, do the action, query the database, etc. !Check if the result matches the expectations.

ARRANGE ACT ASSERT

function test_get_single_role_by_user_query() { // Arrange $this->factory->user->create_many( 2, array( 'role' => 'subscriber' ) ); !!!!!!

}

ARRANGE ACT ASSERT

function test_get_single_role_by_user_query() { // Arrange $this->factory->user->create_many( 2, array( 'role' => 'subscriber' ) ); ! // Act $wp_user_search = new WP_User_Query( array( 'role' => 'subscriber' ) ); $users = $wp_user_search->get_results(); !!!}

ARRANGE ACT ASSERT

function test_get_single_role_by_user_query() { // Arrange $this->factory->user->create_many( 2, array( 'role' => 'subscriber' ) ); ! // Act $wp_user_search = new WP_User_Query( array( 'role' => 'subscriber' ) ); $users = $wp_user_search->get_results(); ! // Assert $this->assertEquals( 2, count( $users ) ); }

ASSERTION

A way of explicitly checking the assumptions that your code makes. !assertEqual() assertTrue() assertNotNull() assertContains() assertGreaterThan() !http://phpunit.de/manual/3.8/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.assertions

EXAMPLE

Q&A

Twitter @cageNL WordPress & Github: barrykooij 10 & 11 mei WordCamp NL