Successfully reported this slideshow.

Unit testing @ WordPress Meetup Tilburg 7 januari 2014

1,267 views

Published on

Unit testing @ WordPress Meetup Tilburg 7 januari 2014

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

Unit testing @ WordPress Meetup Tilburg 7 januari 2014

  1. 1. UNIT TESTING #wpm013
  2. 2. BARRY KOOIJ Senior Web Developer @ Yoast Plugin developer What The File Sub Posts Contributor EDD EDD extensies Moderator WPNL forum Twitter: @cageNL
  3. 3. MIJN SETUP Device IDE Versiebeheer ENV MacBook Pro PhpStorm GIT, GitHub Vagrant w/ VVV Varying Vagrant Vagrants
  4. 4. WHAT
  5. 5. WHAT ARE UNIT TESTS “An automated piece of code that invokes your application code to check a single assumption.” — Pirate Dunbar
  6. 6. MANUAL TESTING Very time consuming High chance on errors Single point of knowledge
  7. 7. UNIT TESTING Instant / very fast feedback Low chance on errors Knowledge put in code
  8. 8. HOW
  9. 9. 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;
  10. 10. TWO WAYS OF UNIT TESTING Write your tests, then write code to make your tests pass Write your code, then write tests that pass
  11. 11. 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
  12. 12. 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)
  13. 13. 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.
  14. 14. ARRANGE ACT ASSERT function test_get_single_role_by_user_query() { // Arrange $this->factory->user->create_many( 2, array( 'role' => 'subscriber' ) ); ! ! ! ! ! ! 
 }
  15. 15. 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(); ! ! ! }
  16. 16. 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 ) ); }
  17. 17. 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-forphpunit.html#writing-tests-for-phpunit.assertions
  18. 18. EXAMPLE
  19. 19. Q&A Twitter @cageNL WordPress & Github: barrykooij 10 & 11 mei WordCamp NL

×