[wp-trac] [WordPress Trac] #17749: Faster and phpunit runner-compatible unit tests

WordPress Trac wp-trac at lists.automattic.com
Fri Jun 10 13:25:47 UTC 2011


#17749: Faster and phpunit runner-compatible unit tests
-----------------------------+-----------------------------
 Reporter:  nbachiyski       |      Owner:
     Type:  feature request  |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  Unit Tests       |    Version:
 Severity:  normal           |   Keywords:  has-patch
-----------------------------+-----------------------------
 Currently the WordPress unit-tests use a custom test runner and most of
 the test cases replace all the data in the database tables before they
 run.

 Since we are using PHPUnit, we should use the phpunit test runner. This
 gives us: in-editor test running, CI, easier creation of more fine-grained
 suites. All these will make running the testt easier, which is a good step
 towards more people running them.

 Replacing the data before each testcase run is often slow. The data isn't
 replaced after each test withing the case, though, which is highly error-
 prone, because we need to account for any changes of the data made during
 the tests.

 Here is my take on solving these problems:

  * Create a very simple PHPUnit test suite, which loads all test classes
 from the tests directory.
  * Install WordPress once, before running any tests.
  * Make all the tables InnoDB.
  * In the default WordPress test case class start a transaction on
 {{{setUp}}}.
  * {{{ROLLBACK}}} all the changes on {{{tearDown}}}.
  * Reinitalize some global variables and clean cache on {{{setUp}}}.

 This way we install WordPress only once (fast) and at the same time we
 don't need to care about the changes we make during each test (not error
 prone).

 This approach, however, doesn't favor having prepared datasets in advance
 (shared fixtures). Which isn't bad. I strongly believe that shared
 fixtures more harm than good. They create unneeded dependencies between
 data, make test unclear, because the data lives far away, and are very
 sensitive to change, because many tests use the same data. If the tests
 are small enough (they should) fresh fixtures won't slow the test, the
 data will be in the test itself and no external data change would break
 them.

 In order to implement the test class and suite, a couple of changes to
 core were needed:
  * WordPress is loaded in a function call (that's how {{{phpunit}}} loads
 tests), so some variables need to be explictly initialized as globals (I
 guess there are more, which will be revaled after more test coverage).
  * Some filters were needed in order to be able to make the tables InnoDB
 (easier to implement than changing the enging after creation).

 The patch is attached.

 The test case class and runner code is at https://github.com/nb/wordpress-
 tests (the current repo doesn't have branch structure, otherwise I would
 have put it there).

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/17749>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list