1

I was wondering is there a way that phpunit for woprdress do not delete any data which i created during test?

0

Usually, you don't want to do this, as it might inadvertently affect tests that run after the test that creates the data.

If there is some data that you need to be available to several of your tests in a single testcase, you can create it with wpSetUpBeforeClass(), and then remove it again in wpTearDownAfterClass().

For example, the REST API category controller test from core:

class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcase {
    protected static $administrator;
    protected static $subscriber;

    public static function wpSetUpBeforeClass( $factory ) {
        self::$administrator = $factory->user->create( array(
            'role' => 'administrator',
        ) );
        self::$subscriber = $factory->user->create( array(
            'role' => 'subscriber',
        ) );
    }

    public static function wpTearDownAfterClass() {
        self::delete_user( self::$administrator );
        self::delete_user( self::$subscriber );
    }

    // [snip]
}

Otherwise, if you just want to keep some database changes from a test, perhaps for debugging purposes, you can use commit_transaction().

    public function test_something() {

        do_something();

        $this->commit_transaction();
    }
2
  • Actually i am trying to insert some complex test data in posts and postmeta table that needs carefull validation that test data is infact valid and i want to check it manually by inspecting the tables. Now if the one of the test data generation function out of several function (which run one after one) is not inserting the correct data then i have to run the test again and again and it wastes lots of time as every execution of phpunit takes time ,So i f i can continue from where i left in last test i can save some time, I wonder if there is some flag which i can use or some function – chown Aug 5 '17 at 13:00
  • @chown You could do like in my second example and commit the transaction after each data generation function runs. Then you could use xdebug and set a breakpoint after your first data generation function, and you'd essentially pause the tests while you inspected the database manually. Or, if you don't have xdebug set up, you could commit the transaction and then exit. But I don't think there is a way to retain the data between runs of phpunit, because WP recreates its db tables each time. – J.D. Aug 5 '17 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.