DEV Community

Ariel Mejia
Ariel Mejia

Posted on

Fix Allowed memory size Exceptions in PHPUnit Tests

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.

Top comments (1)

Collapse
 
po0q profile image
pO0q 🦄 • Edited

while this is possible, devs should be aware that memory consumption can be a concern. So, it's totally fine to increase the memory limit, as long as you have valid reasons.