Code coverage How much of the non-test code is exercised by test cases 100% coverage? Depends on project priorities Pragmatic, not dogmatic WordPress core has plenty of room for improvement ◦ Patches!
Frameworks PHP: PHPUnit, SimpleTest, Behat JavaScript: QUnit, Jasmine, Mocha And many more, for almost every programming language For UI automation: Selenium, Watir For headless browser testing: Phantom.js
Anatomy of a test Class extending PHPUnit_Framework_TestCase At least one test method Optional setUp and tearDown ◦ Shared among all test methods in the class setUp testMethod •Assertion 1 •Assertion 2 •… tearDown
Stubs & Mocks When testing a unit, replace any dependencies with fakes that are fully under control of the test Stubs ◦ Replaces all methods with stubs ◦ You configure return values of any methods Mocks ◦ Verify the behavior of interactions between unit and other objects ◦ Beware of coupling test to implementation
Running tests Usage: phpunit [switches] UnitTest [UnitTest.php] phpunit [switches] --filter Filter which tests to run. --group ... Only runs tests from the specified group(s). --exclude-group ... Exclude tests from the specified group(s). --list-groups List available test groups. --repeat Runs the test(s) repeatedly. --colors Use colors in output. --stderr Write to STDERR instead of STDOUT. --stop-on-error Stop execution upon first error. --stop-on-failure Stop execution upon first error or failure. --stop-on-skipped Stop execution upon first skipped test. --stop-on-incomplete Stop execution upon first incomplete test. --strict Run tests in strict mode. -v|--verbose Output more verbose information. --debug Display debbuging information during test execution. --process-isolation Run each test in a separate PHP process. --bootstrap A "bootstrap" PHP file that is run before the tests. -c|--configuration Read configuration from XML file. --no-configuration Ignore default configuration file (phpunit.xml). --include-path Prepend PHP's include_path with given path(s). -d key[=value] Sets a php.ini value. -h|--help Prints this usage information. --version Prints the version and exits. --debug Output debugging information.
WP_UnitTestCase setUp ◦ Open database connection ◦ Create WP_UnitTest_Factory ◦ Clean up global scope ◦ Clear $_GET, $_POST, flush object cache ◦ Beware global scope – avoid constants (see multisite, ajax) ◦ Start a DB transaction tearDown ◦ Rollback the DB transaction Correlates @ticket annotation to Trac tickets and skips tests for known issues ◦ Works for Core, Unit Tests, and Plugin tickets
How can you help? Fix failing test cases Improve code coverage Write patches for core tickets that are causing skipped/failed tests ◦ needs-unit-tests tickets ◦ Tests for any core bugs Does your plugin use a core API or action? Write a test for it so that core devs do not unintentionally break your use-case. Write tests for your plugins!