Skip to content

Core Concepts

Liam Defty edited this page Jul 7, 2020 · 3 revisions

Automated tests tend to consist of four steps:

  1. Setup
  2. Exercise
  3. Verification
  4. Teardown

Step One: Setup

In the first step, setup, we create the data and environment that the test needs in order run. WP Cypress handles creating an environment but we may also need to perform additional set up before specific tests or populate the database with test data. To help with this, WP Cypress allows you to seed the database using seeds and fixtures. Seeds execute PHP in the running container allowing you to perform any required logic you need to populate your database with data. Alongside this you can use fixtures as a way to create data in a structured way.

Head to the documentation on Seeds and Fixtures and to learn more about how you can use them.

Step Two: Exercise

In the second step, exercise, we walk through the steps that are necessary to get the application into the state we’re interested in. Cypress provides helpful commands to help get the application into the desired state. On top of this, WP Cypress provides additional commands that may prove useful within the context of a WordPress application. For example, if you wish to test functionality within the WordPress editor you may find the editPost command useful to visit a specific posts edit page:

describe('Visit a post', () => {
  before(() => {
    cy.editPost(1);
  });
});

For a list of available commands provided by WP Cypress, please refer to Cypress Commands.

Step Three: Assertion

In the third step, assertion, we ask, “Did the code do the thing it was expected to do?”. Cypress bundles the Chai assertion library bringing you many assertions to help you verify that your application is behaving the way you'd expect. To see what assertions are available please visit the Cypress documentation

Step Four: Teardown

Finally, in teardown, we put the application back the way we found it before running any more tests. In WP Cypress this is handled before each describe block so you don't have to worry about it. This process consists of:

  1. Ensuring the correct version of WordPress are running for the current block of tests.
  2. Re-setting the database.
  3. Re-installing WordPress.
  4. Re-activating the relevant themes and plugins.
  5. Re-running the DefaultSeeder seed.