Ace's avatar
Level 7

Slow Test Speeds on local machine using Sail and Test/Pest

I am testing pest running a simple test on the fortify register route: It's taking between 7-8 seconds for that one test.

I am using laravel 10 and Sail locally

your advise

0 likes
2 replies
LaryAI's avatar
Level 58

There are a few potential reasons why your tests might be running slowly on your local machine using Sail and Test/Pest. Here are some possible solutions to improve the test speed:

  1. Optimize Database Setup: Make sure you are using an in-memory database like SQLite for your tests. This can significantly speed up the test execution time. You can configure this in your phpunit.xml file.

  2. Disable Debugging: Disable any debugging tools or features that might slow down the test execution. For example, if you have Xdebug enabled, consider disabling it for your tests.

  3. Use Test Databases: Instead of using your production database, create separate test databases for your tests. This will prevent any interference between your tests and your actual data.

  4. Reduce Test Data: Minimize the amount of data you create or load for your tests. Only create the necessary data required for each test case. This will help reduce the time it takes to set up the test environment.

  5. Optimize Test Code: Review your test code and make sure it is efficient. Avoid unnecessary database queries or expensive operations. Use test doubles or mocks when appropriate to isolate dependencies and speed up the tests.

  6. Parallelize Tests: If your tests are independent of each other, consider running them in parallel. This can be done using tools like PHPUnit's --parallel option or Pest's parallel execution feature.

  7. Use Caching: If your tests involve repetitive operations, consider using caching to speed up the execution. For example, you can cache expensive database queries or API responses.

Here's an example of how you can configure an in-memory SQLite database for your tests in your phpunit.xml file:

<php>
    <server name="DB_CONNECTION" value="sqlite"/>
    <server name="DB_DATABASE" value=":memory:"/>
</php>

Remember to adjust the configuration based on your specific needs and environment.

Implementing these optimizations should help improve the speed of your tests.

MrMoto9000's avatar

Having the same issue. Vanilla PHPUnit tests don't seem to suffer the same slowdown with Sail. Single tests can be 4x/5x faster than Pest versions of the same test. Even something like true === true takes forever on Pest/Sail.

Please or to participate in this conversation.