Getting Started with Unit Testing





A bit about me



  • SFNdesign <- my agency
    • running it for 6 years
    • WordPress eCommerce and Membership
    • usually high scale stuff with lots of externals
  • curtismchale.ca <- personal site (business topics)
  • WP Theme Tutorial <- dev centric WP topics

What is Unit Testing




    Writing code to test your code

      TDD, BDD, Regression Tests

      Only PHPUnit today use QUnit for JS testing

      Why would I write tests?












      image via: nukamari



      Really Why!

      Saves Time - old workflow


      • write code
      • browser
      • add data
      • save
      • check for output (WP_DEBUG on!)
      • click around to check other stuff
      • deal with clients finding problems later

      Saves time - New Workflow



      • write a test for your code
      • write the code
      • run your tests
      • check results
      • move on

      Save time - New code



      Remember Curtis M


      make sure it doesn't break old code

      Save Time - Refactoring Safely





      make it better without breaking

      Save Time - Initial Costs




      Yes it does take a bit longer to start

      but all those long term benefits

      Simpler Testable Code



      • higher number of smaller functions



      • no more monolithic 900 line functions with logic, templates...

      Provides some docs





      Future developers can use the tests on code they don't know

      Test First or last?



      Pure TDD says write first


      Write last is Regression Testing


      Tools of the trade

      PhPunit


      This is a unit testing framework for WordPress


      • $this->assertTrue( $a === $b );
      • $this->assertArrayHasKey( 'foo', array( 'foo' => 'bar' ) );
      • $this->assertNull( NULL );
      • PHPUnit Docs

      WP_Unittestcase


      WordPress API that extends PHPUnit



      WARNING: Don't run the core tests on a live or dev site

      WP_Unittestcase

      setUp & tearDown

      WP_unittestcase

      post creation



      WP_Unittestcase


      users



      Mock roles for plugins like WooCommerce

      WP_unittestcase


      term creation


      WP_Mock


      Alternative to the WordPress->factory

      Mimics the WordPress API
      • functions
      • filters
      • actions
      • install with Composer

      WP_Mock


      • more abstract


      WP_CLI


      It's simply awesome so use it


      Can build out your starting tests for a plugin


      wp scaffold plugin-tests $pluginname

      VVV


      WHOA that's a lot of stuff!!!

      Just use VVV as your dev environment


      Nope:
      For Windows use Composer
      All the windows stuff I found said use PEAR

      Travis-ci


      Continuous Integration


      • checks branches
      • checks pull requests

      Now Meet c. McHale












      image via: great8

      Some basic principles



      • test success
      • test failures
      • test the 'public' API
      • cover as much as possible


      More Code Time

      RCP Email Passwords



      Emails passwords to users imported by RCP CSV Import

      WordPress Test Patterns






      Questions???

      Resources: Presentations


      Resources: Books



      Resources: Blog Posts


      Getting Started with Unit Testing

      By curtismchale

      Getting Started with Unit Testing

      • 9,105