Plugin development with unit tests

The unit tests transform all CREATE TABLE and DROP TABLE queries to CREATE TEMPORARY TABLE and DROP TEMPORARY TALBE, respectively. So in your tearDown the query will attempt to drop temporary tables with those names, but not the actual tables. To fix this, add this before your DROP queries:

remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );

You may also need to add this before your CREATE TABLE queries:

remove_filter( 'dbdelta_create_queries', array( $this, '_create_temporary_tables' ) );

The WP_UnitTestCase::_create_temporary_tables() function is the one that filters the queries to make tables temporary. It is added in the setUp(), and removed in the tearDown().

Update:

The _create_temporary_tables() function is now hooked to the 'query' filter, instead of 'dbdelta_create_queries'. See changeset 27041. So now to remove it you would do:

remove_filter( 'query', array( $this, '_create_temporary_tables' ) );

Leave a Comment