Faster WordPress acceptance testing in Codeception – 02

In an earlier post I’ve presented a basic WordPress-oriented extension of the PhpBrowser Codeception class.
Although working used alone it will generate some strong test coupling and inter-dependency issues in cases where one test will fail, and hence return, without restoring the original state.

Before, after and failed

Cest format tests allow for more flexibility over cept format tests thanks to _before, _after and failed methods but still do not allow conditionals in the tests to implement a dynamic pre-condition setting.
As an example a test like this one to check for dependent plugin activation

$I = new AcceptanceTester($scenario);

// describe
$I->am('an administrator');
$I->wantTo('activate Plugin B with Plugin A installed and activated');

// set up the test pre-conditions
$I->activatePlugin('plugin-a'); // could fail here but still pre-condition!

// test

// reset, might never happen and screw following tests

could fail because “plugin-a” is activated already, the activation link is not found and the test will fail before any real testing began.
But there a reason and a solution to it.

Easy solution

Since WordPress is a stateful machine persisting its state to a database resetting that same database will reset WordPress to a known state; Codeception allows that in an easy way using the Db module.
To allow for that soft-site-reset I’ve modified the Vagrant machine to allow for external connections to the database and added the Db module to acceptance test.
That’s done modifying the /tests/acceptance.suite.yml file to include the module and its configuration

# Codeception Test Suite Configuration

# suite for acceptance tests.
# perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.

class_name: AcceptanceTester
        - AcceptanceHelper
        - WPBrowser
        - Db
            url: 'http://test-site.local'
            adminUsername: 'admin'
            adminPassword: 'admin'
            adminUrl: '/wp-core/wp-admin'
            dsn: 'mysql:host=;dbname=testDb'
            user: 'root'
            password: 'root'
            dump: 'tests/_data/dump.sql'
            populate: true
            cleanup: false

and rebuilding the AcceptanceTester class after that with

codecept build

I will now get totally independent acceptance tests separated by a thorough database reset.