WordPress unit test clean up methods

I’ve added more tests to Route Pages WordPress plugin to make sure everything works as expected and had some head scratching while dealing with WP_UnitTestCase class setUp and tearDown methods.

The problem

Testing route-driven post creation I’ve had an issue with test inter-dependency generated by poor clean up between them; in a use case like this one

/**
 * @test
 * it should create a page if good route meta has been persisted
 */
public function it_should_create_a_page_if_good_route_meta_has_been_persisted() {
    // trigger route post generation
    add_filter( RoutePages::SHOULD_GENERATE_ROUTE_POSTS, '__return_true' );
    // set the route meta in the database
    $title     = 'Waldo';
    $routeMeta = array(
        'waldo' => array(
            'title' => $title,
            'permalink' => '^waldo$',
            'generate' => 'page'
        )
    );
    add_option( WPRouting_PersistableRoute::OPTION_ID, $routeMeta );
    // make sure the page does not exist
    $this->assertNull( get_page_by_title( $title ) );
    // trigger page generation
    $sut = new RoutePages_PageManager();
    $sut->maybeGenerateRoutePages();
    // check for the page to be there
    $page = get_page_by_title( $title );
    $this->assertNotNull( $page );
    $this->assertEquals( $title, $page->post_title );
    $this->assertEquals( '', $page->post_content );
    $this->assertEquals( 'waldo', $page->post_name );
} 

/**
 * @test
 * it should not have any pages in the database at first
 */
public function it_should_not_have_any_pages_in_the_database_at_first() {
    $this->assertCount( 0, get_posts( array( 'post_type' => 'page' ) ) );
} 

the second test will fail when executed after the first: the page created in the first test is still there after it ran.
Since I’m using WP Loader to run the tests I’ve added a setUp method to the test case to take care of the issue:

public function setUp() {
    delete_option( WPRouting_PersistableRoute::OPTION_ID );
    delete_option( RoutePages::META_OPTION_NAME );
    global $wpdb;
    $wpdb->delete( $wpdb->prefix . 'posts', array( 'post_type' => 'page' ) );
    $wpdb->delete( $wpdb->prefix . 'posts', array( 'post_type' => 'post' ) );
}

but that did end up in bad $wpdb usage errors
wpdb not used properly message

The solution

Simply calling WP_UnitTestCase methods in test case specific setUp and tearDown methods will solve the issue:

public function setUp() {
    // WP_UnitTestCase set up 
    parent::setUp();
    // my set up code here
    delete_option( WPRouting_PersistableRoute::OPTION_ID );
    delete_option( RoutePages::META_OPTION_NAME );
    global $wpdb;
    $wpdb->delete( $wpdb->prefix . 'posts', array( 'post_type' => 'page' ) );
    $wpdb->delete( $wpdb->prefix . 'posts', array( 'post_type' => 'post' ) );
} 

public function tearDown() {
    // my tear down code here
    parent::tearDown();
}

and reward me with green lights
Route Pages page creation test greenlights

Bonus

Since the two methods above are quite useful I’ve updated WP Browser package to scaffold WP_UnitTestCase extension classes with ready-to-use and helpful setUp and tearDown templates.

Probably related

I appreciate your input