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

zufflie's avatar

Tests get stuck and time out after using Refreshdatabase a few times.

Hello dear Laracasts Forum.

I have a weird problem, searched SO, Here, but could not find help.

When i start my IDE (Phpstorm) und boot my system, i can run all the tests just fine. But after a while, my tests who uses this Trait will timeout and i cant continue doing TDD until i eventually reboot my System, which as you can imagine is super annoying.

I use .env.testing file DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=dbname_test DB_USERNAME=root DB_PASSWORD=

I tried sqlite etc, nothing really helps.

Does anyone has an idea what could that be? Or how i can look further into the issue?

0 likes
1 reply
LaryAI's avatar
Level 58

It sounds like you're encountering a problem where your tests are hanging after using the RefreshDatabase trait multiple times. This trait is used in Laravel to migrate the database before each test, ensuring that you have a fresh state. However, if your tests are getting stuck, there could be several reasons for this. Here are some steps you can take to troubleshoot and potentially resolve the issue:

  1. Check Database Configurations: Ensure that your .env.testing configuration is correct and that the database specified is different from your local or production database to avoid any accidental data loss.

  2. Optimize Migrations: If you have a large number of migrations, it might take a long time to run them all. Consider consolidating migrations if possible.

  3. Database Cleanup: Make sure that the database is properly cleaned up after each test. Sometimes, leftover data or connections can cause subsequent tests to hang.

  4. Increase Timeout: If the timeout is too short for your migrations to complete, you can increase the timeout setting in your PHPUnit configuration.

  5. Check for Deadlocks: Ensure that there are no database deadlocks occurring. You can check your database logs for any deadlock information.

  6. Use Database Transactions: Instead of using RefreshDatabase, you could use the DatabaseTransactions trait, which is faster as it only rolls back transactions after each test, rather than migrating the database.

  7. Check for Open Connections: Make sure there are no lingering database connections that are not being closed properly.

  8. Monitor System Resources: Check if your system is running out of resources (CPU, memory) after running tests multiple times.

  9. Debugging: Add debugging statements in your tests to see where they are getting stuck. You can also use tools like xDebug to step through your tests and find the bottleneck.

  10. Restart Services: Sometimes, restarting the database service can clear up any issues that are causing the tests to hang.

Here's a sample PHPUnit configuration to increase the timeout:

<phpunit>
    <!-- ... other configurations ... -->
    <php>
        <ini name="max_execution_time" value="120"/>
    </php>
</phpunit>

And here's how you might use the DatabaseTransactions trait instead of RefreshDatabase:

<?php

namespace Tests;

use Illuminate\Foundation\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    use DatabaseTransactions;

    // Your tests...
}

If none of these steps resolve the issue, you may need to provide more specific information about your test setup or any error messages you're receiving for further assistance.

Please or to participate in this conversation.