I have a lot of tests and running all of them takes a long time ~15 minutes. This is mainly due to a lot fo the tests building a new sqlite database and seeding it.
A lot of my tests don't change the database, so they could all be run on the same database, which is created just once. However, I can't figure out how to setup my tests to work like this.
I use an in memory sqlite, in Laravel.
I am trying to stop my phpunit tests from creating and seeding the database every time.
My latest attempt is to use the trait detailed here: https://stackoverflow.com/a/57788123/42106
However, when I run my tests, the first test passes fine (so the database tables exist) then the 2nd test in the file fails with: "General error: 1 no such table: users".
./bin/phpunit ./tests/Auth/UserTest.php
So the database tables have been wiped after the first test.
I have tried overriding the tearDown method but it makes no difference.
What could be wiping my database?
<?php
namespace Tests\Auth;
use Tests\TestCase;
use Tests\MigrateFreshAndSeedOnce;
use App\Entity\Models\User;
class UserTest extends TestCase
{
use MigrateFreshAndSeedOnce;
public function testUser1()
{
$user = User::where('id', 1)->get()->first();
$this->assertTrue($user->id !== null);
}
public function testUser2()
{
$user = User::where('id', 2)->get()->first();
$this->assertTrue($user->id !== null);
}
}
Here is the trait:
<?php
namespace Tests;
use Illuminate\Support\Facades\Artisan;
trait MigrateFreshAndSeedOnce
{
/**
* If true, setup has run at least once.
*
* @var boolean
*/
protected static $setUpHasRunOnce = false;
/**
*
* @return void
*/
public function setUp() : void
{
parent::setUp();
if (!static::$setUpHasRunOnce) {
echo '--DB--';
Artisan::call('migrate:fresh');
Artisan::call(
'db:seed',
['--class' => 'DatabaseSeeder'] //add your seed class
);
static::$setUpHasRunOnce = true;
}
}
}
Finally, my TestCase class:
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected $baseUrl = 'http://dev.php73.mysite.com:8888';
}
My phpunit env vars:
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="testing" />
</php>
Thanks!
use RefreshDatabase;trait? Why do you need to persist the data in memory/sharing?