0

I'm trying to set up testing using pest in a laravel project. I have several migrations which are stored in ./test/database/migrations, and I'd like to spin up a new sqlite in-memory database using those migrations prior to running tests. This seems like a fairly standard set-up, but I can't seem to get it to work.

my phpunit.xml contains this:

    <env name="DB_CONNECTION" value="sqlite_testing" />

config/database.php

    'sqlite_testing' => [
      'driver'   => 'sqlite',
      'database' => ':memory:',
      'prefix'   => '',
    ],

It seems that it is able to connect to the database, but none of the migrations have run. I get errors like this:

SQLSTATE[HY000]: General error: 1 no such table: users (Connection: sqlite_testing, SQL: select * from "users" where "users"."deleted_at" is null limit 1)

I tried to run the migrations in the test by doing this:

beforeAll(function () {
  Artisan::call('migrate', ['--path' => "tests/database/migrations"]);
});
3
  • Is the test setup in a CI/CD pipeline? Or are you manually testing this? Commented Apr 17 at 23:55
  • this is manual testing Commented Apr 18 at 13:29
  • Alright got it. Commented Apr 18 at 13:58

2 Answers 2

2

I hope this helps someone. In order to run migrations from a particular directory, I had to edit /tests/TestCase.php and add the options to migrateFreshUsing() like this:

abstract class TestCase extends BaseTestCase
{
  use RefreshDatabase;

  protected function migrateFreshUsing()
  {
    return [
      '--path' => 'tests/database/migrations',
      '--database' => 'sqlite_testing',
    ];
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can try adding this line of code in your pest test

<?php
 
use Illuminate\Foundation\Testing\RefreshDatabase; // add this
 
uses(RefreshDatabase::class); // add this

test('test my code', function () {
});

2 Comments

This doesn't work. How do I tell it to run my migrations from a particular directory (tests/database/migrations instead of default)? Also, This creates a bunch of errors because my permissions table is not created when it tries to boot the app.
Also, I want it to run the migrations only once. I want it to create the in-memory database at start of testing, not before each test.

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.