PhpUnit与Zend Framework内存问题

I've just started using PHP Unit with Zend Framework and I have a problem with the memory_limit. Currently, the memory limit is set to 32M and when running all tests I get an fatal error:

Allowed memory size of 33554432 bytes exhausted.

Currently, I have: 7 files, 125 tests, 332 assertions. I only test the controllers. I use @dataProvider for some tests, with the 1 to 5 sets of data (at first I was reading them from file, I've tried to put the data directly in the tests files, but it still hits the memory limit)

So my question is what is your usual memory limit for phpunit based testing? Where should I look to improve the memory testing? Any advice will be great.

Thanks, Gabriel

I've run into a similar issue after my test suite exceeded 300 cases with 128M memory limit.
What helped me was running test cases in isolated processes. You can do this either from command line:

phpunit --process-isolation

or add it to your phpunit.xml config:

<phpunit bootstrap="./application/bootstrap.php"
    processIsolation="true">

I run PHP 5.3.3 with the default memory setting (128M) and never had an issue with unit testing applications on Zend Framework.

Another Solution would be to turn off backing up and restoring the global state after every test. PHPUnit does this by default, this reduced the Memory Usage of our Test Suite from 1000 MB to 90 MB ... (yeah we have a big fat application)

There is a setting called backupGlobals in the phpunit configuration file.

https://phpunit.readthedocs.io/en/7.4/configuration.html

600 Tests, ? Assertions, 80 000 lines of code, around 1200 files without libraries

For an old application, I had a problem of memory increasing after each test, resulting in more than 1G memory usage. To solve that I tried a lot of different things. What worked best were:

  1. clean up variables of your test classes at tearDown().
  2. identify the resources loaded at bootstrap(), class resources and plugin resources. One of them was the cause of the memory increase. Finding the code and fixing it decreased the usage 10x.
  3. Enable garbage collection and call gc_collect_cycles() at tearDown().

Be carefull of doing phpunit --process-isolation because it runs each test in a separate PHP process, so test will be much much slower. Instead you can just increase memory_limit in your php.ini