Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

UniqueHope's avatar

Tests emptying database after installing Pest

So tried to install pest to my docker laravel setup and what I ended up doing running was: sail composer require pestphp/pest --dev --with-all-dependencies and then just to be safe I also did: sail artisan pest:install But now when I run the old tests something is deleting the values on all the tables. The simplest new test is fine:

<?php

namespace Tests\Unit;

beforeEach(function () {
    var_dump('before');
});

test('example', function(){
    //loginSelf()->
    fwrite(STDERR, print_r("Test start"."\n", true));
    $this->assertTrue(true);
});

If I comment out the name of the test (and everything in it) then it's fine, if it's just the content then it's still deleting everything:

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use App\Models\Client;

class ClientTest extends TestCase
{

    use DatabaseTransactions;

    /**
     * Test info
     *
     * @return void
     */
    public function testname()
    {
	//
    }
}

Is use Tests\TestCase; doing something, since that's changed?

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    use LazilyRefreshDatabase;
}
0 likes
3 replies
LaryAI's avatar
Level 58

It looks like the issue you're encountering is related to the database transactions and the LazilyRefreshDatabase trait. When using Pest with Laravel, the LazilyRefreshDatabase trait can cause the database to be refreshed between tests, which might be why you're seeing your tables being emptied.

To address this, you can try the following steps:

  1. Remove the LazilyRefreshDatabase trait from your TestCase class if you don't want the database to be refreshed between tests.

  2. Use DatabaseTransactions in your test classes to ensure that each test runs within a database transaction, which will be rolled back after the test completes.

Here's how you can adjust your TestCase and ClientTest classes:

TestCase Class

Remove the LazilyRefreshDatabase trait:

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    // Remove LazilyRefreshDatabase trait
}

ClientTest Class

Ensure you are using the DatabaseTransactions trait:

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use App\Models\Client;

class ClientTest extends TestCase
{
    use DatabaseTransactions;

    /**
     * Test info
     *
     * @return void
     */
    public function testname()
    {
        // Your test code here
    }
}

Example of a Pest Test

If you want to use Pest with database transactions, you can do something like this:

<?php

use Illuminate\Foundation\Testing\DatabaseTransactions;

beforeEach(function () {
    // This will ensure each test runs within a transaction
    $this->beginDatabaseTransaction();
});

test('example', function () {
    // Your test code here
    $this->assertTrue(true);
});

By ensuring that your tests run within transactions and removing the LazilyRefreshDatabase trait, you should be able to prevent the database from being emptied between tests. This approach will help maintain the state of your database as expected during testing.

UniqueHope's avatar

There are some other things I'm running into trying to check this, but the suspicion is that this new setup requires a .env.testing file and by that a separate DB for testing even though it has worked fine as is before trying Pest? Could anyone confirm this for me please?

Tray2's avatar

You should almost always start with an empty database when you run your test suites. So you should cofigure a separate database for test, and not use your dev database for them.

Please or to participate in this conversation.