2

I want to test my Laravel 4 applications. But I can't do it without creating database. The problem is, that I want to approach these things:

  • Separate DB for tests and for development (in memory also will be good)
  • Doing migrations and seeds before testing
  • Clearing database after testing

How to approach that?

1 Answer 1

6

I think this will help you get started

1. Override Database settings for testing only

Inside app/config/testing create a new file named database.php and inside it place:

return array(

  'default' => 'mysql',

  'connections' => array(

    'mysql' => array(

      'driver' => 'mysql',

      'host' => 'localhost',

      'database' => 'TEST_DB_NAME', // Create also the Database

      'username' => 'USERNAME',

      'password' => 'PASSWORD',

      'charset' => 'utf8',

      'collation' => 'utf8_unicode_ci',

      'prefix' => '',

    )
  )
);

This database will bee used only for testing purposes.

2. Add this info to codeception.yml also

modules:

  config:

    Db:

      dsn: 'mysql:host=localhost;dbname=TEST_DB_NAME'

      user: 'USERNAME'

      password: 'PASSWORD'

      dump: app/tests/_data/dump.sql

3. Migrate and seed the testing database

php artisan migrate --seed --env="testing"

4. To clean up the database

php artisan migrate:reset --env="testing"
Sign up to request clarification or add additional context in comments.

1 Comment

I needed to skip migrate:reset, because Codeception automatically resets database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.