behat Getting started with behat Setting up test data


With functional testing data is often modified. This can cause subsequent runs of the test suite to fail (as the data could have changed from the original state it was in).

If you have setup your data source using an ORM or framework that supports migration or seeding (like Doctrine, Propel, Laravel), you can use this to create a new test database complete with Fixture data on each test run.

If you don't currently use one of these (or equivalent), you can use tools like Phinx to quickly setup a new test database or prepare an existing database for each test run (clean up test entries, reset data back to it's original state).

# Install Phinx in your project
$ php composer.phar require robmorgan/phinx

$ php vendor/bin/phinx init
Phinx by Rob Morgan - version x.x.x
Created ./phinx.xml

Add your database credentials to ./phinx.xml.

$ php vendor/bin/phinx create InitialMigration

You can specify how your database tables are created and populated using the syntax provided in the documentation.

Then, each time you run your tests you run a script like this:

#!/usr/bin/env bash

# Define the test database you'll use

# Clean up and re-create this database and its contents
vendor/bin/phinx migrate

# Start your application using the test database (passed as an environment variable)
# You can access the value with $_ENV['database']
database=$DATABASE php -d variables_order=EGPCS -S localhost:8080

# Run your functional tests

Now your functional tests should not fail due to data changes.