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
  • <- 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


      This is a unit testing framework for WordPress

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


      WordPress API that extends PHPUnit

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


      setUp & tearDown


      post creation



      Mock roles for plugins like WooCommerce


      term creation


      Alternative to the WordPress->factory

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


      • more abstract


      It's simply awesome so use it

      Can build out your starting tests for a plugin

      wp scaffold plugin-tests $pluginname


      WHOA that's a lot of stuff!!!

      Just use VVV as your dev environment

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


      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


      Resources: Presentations

      Resources: Books

      Resources: Blog Posts

      Getting Started with Unit Testing

      By curtismchale

      Getting Started with Unit Testing

      • 8,435