Fix Allowed memory size Exceptions in PHPUnit Tests

Ariel Mejia - Dec 15 '23 - - Dev Community

Problem

Sometimes a test suit does not complete to run the whole tests as it throws an exception: "Allowed memory size"

Cause

PHPUnit runs tests in your local environment with some default memory limit, even when you can override your memory_limit in php.ini file, it reflects an increase when app is running, but PHPUnit default config would override this when tests runs.

Solution

Luckly, PHPUnit provide an easy way to fix this by adding phpunit variables to the phpunit.xml file:

<ini name="memory_limit" value="512M"/>
Enter fullscreen mode Exit fullscreen mode

Take in mind

This issue is complicated to see by running PHPUnit Tests with a GitHub Action Workflow, this is because the action would set for usual applications a higher memory limit, in this case you should take that in mind because even when your tests are totally fine the memory consumption is set in different ways for every environment.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .