16

I'm currently using Zend Framework in conjunction with PHPUnit to conduct unit testing on an application. When Hudson executes the PHPUnit shell command, the maximum PHP memory limit is reached sometime during code coverage generation. I currently have a total of 41 tests with 334 assertions.

I have successfully eliminated this error by raising the memory_limit setting to 768M using the -d memory_limit=768M switch; however, I am worried that as the complexity increases along with the total number of tests/assertions, I will not have enough memory to generate the HTML for code coverage statistics.

OS: CentOS 5.5
Control Panel: WHM/cPanel
CI Server: Hudson

/usr/local/bin/phpunit 
  --verbose  
  -d memory_limit=512M  
  --log-junit ../../build/logs/phpunit.xml   
  --coverage-clover ../../build/logs/coverage/clover.xml   
  --coverage-html ../../build/logs/coverage-html/  

Fatal error: Allowed memory size of 536870912 bytes exhausted

Before committing my changes and letting Hudson handle the rest, I use Windows 7 for development. The memory usage never exceeded 340MB while running the same command within W7.

hakre
  • 193,403
  • 52
  • 435
  • 836
webjawns.com
  • 2,300
  • 2
  • 14
  • 34
  • Are all of your test cases in one class? – allnightgrocery Oct 09 '10 at 20:36
  • Nope, the tests are divided between four different files. These tests are relatively simple, esp. compared with what we have planned. – webjawns.com Oct 13 '10 at 03:46
  • 1
    :) thanks for posting your solution. I've been having the problem you listed and have been really hacking at it. Every time something would come up (memory exhausted in /pear/phpunit/framework/whatever.php on line 1999) I'd type `sudo vi /.../whatever.php` and add in a line `set_ini("memory_limit","1000M")`. Probably not best practice ;) – Alex C Jan 26 '11 at 16:04
  • No worries, we've all been known to break the rules from time to time. Happy I could help! – webjawns.com Apr 01 '11 at 05:58

4 Answers4

15

By reducing the number of files included within code coverage, as well as increasing the overall memory limit in PHP, I was able to basically kill this error. The entire Zend Framework was being included within code coverage, which is very large.

webjawns.com
  • 2,300
  • 2
  • 14
  • 34
  • To make this easy, I put this stuff in an XML configuration, e.g. "MY_LIBRARY". If you name this phpunit.xml and it's in your current working directory, php unit will apply this automatically. – Mark E. Haase Dec 16 '11 at 22:23
  • Here are two relevant links: [code-coverage-analysis.including-excluding-files](http://www.phpunit.de/manual/current/en/code-coverage-analysis.html#code-coverage-analysis.including-excluding-files), and [XML config file code coverage whitelist filter syntax](http://www.phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.blacklist-whitelist) – KajMagnus Jul 18 '12 at 12:29
  • 1
    Its important to note that initially you may just see phpunit stop executing tests without error and show "Returned: 255" – Josh Woodcock Oct 23 '13 at 13:32
  • 1
    New link to code coverage on PHPUnit: https://phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.blacklist-whitelist – Pepijn Aug 04 '15 at 15:00
2

Do you have xdebug profiling enabled, if so try disabling it. I've experienced this problem before, and it came down to extensions in php (specifically xdebug profiling and/or Inclued heirarchy viewer)

Kayla Rose
  • 1,293
  • 10
  • 12
  • I just checked, and I was hoping that was the issue, but profiling is turned off. Thanks for your response. Any other ideas? – webjawns.com Oct 15 '10 at 03:12
  • It's hard to tell without knowing the codebase. I would just keep an eye out for red flags (e.g. exhaustive __autoloading, caching stuff in memory, etc.) that are not normally a problem, but could cause issues during testing because of high number of instantiations etc. Also take advantage of tearDown() if possible. – Kayla Rose Oct 15 '10 at 15:12
0

As of 2019, you can use the PCOV driver with PHPUnit to generate your code coverage report. In my experience, it's only marginally less performant than running a plain PHPUnit suite.

Read Speed up PHPUnit Code Coverage Analysis for some good benchmark comparing XDebug, PHPDebug and PCOV. It also has instructions on how to enable PCOV on PHPUnit 8.

Read Setup PHP PCOV for 5 times faster PHPUnit code coverage for instructions on setting up PCOV on PHPUnit7 and below.

Maxime Rainville
  • 2,019
  • 23
  • 29
0

I had an OOM problem while using any code coverage format in PHPUnit v10 and have solved it by setting the 'processIsolation' option to true. Maybe this helps someone too.

Marko Ivančić
  • 299
  • 2
  • 4