首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拆分PHPunit测试以停止内存不足错误

拆分PHPunit测试以停止内存不足错误
EN

Stack Overflow用户
提问于 2018-03-06 03:33:32
回答 1查看 3.6K关注 0票数 5

我有一个使用phpunit进行单元测试的商业应用程序。业务应用程序的大小在不断增长,单元测试也在不断增长。当我运行phpunit来获取代码覆盖率报告时,我会耗尽内存。生成包含大量单元测试的代码覆盖率报告的好方法是什么?

运行下面的phpunit.xml,我在完成之前得到了以下错误:

代码语言:javascript
复制
PHP Fatal error:  Allowed memory size of 1073741824 bytes exhausted (tried to allocate 1068290 bytes)

我使用PHP5.6和Jenkins来运行作业。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
  backupStaticAttributes="false"
  bootstrap="./src/bootstrap/autoload.php"
  colors="true"
  convertErrorsToExceptions="true"
  convertNoticesToExceptions="true"
  convertWarningsToExceptions="true"
  processIsolation="false"
  stopOnFailure="false"
  syntaxCheck="false"
>
 <testsuites>
  <testsuite name="module1">
   <directory>./tests/module1</directory>
   <directory>./tests/module2</directory>
   <directory>./tests/module3</directory>
   <directory>./tests/module4</directory>
   <directory>./tests/module5/directory>
   <directory>./tests/module6</directory>
   <directory>./tests/module7</directory>
   <directory>./tests/module8</directory>
  </testsuite>
  <testsuite name="module2">
   <directory>./tests/module9</directory>
   <directory>./tests/module10</directory>
   <directory>./tests/module11</directory>
   <directory>./tests/module12</directory>
   <directory>./tests/module13</directory>
   <directory>./tests/module14</directory>
   <directory>./tests/module15</directory>
   <directory>./tests/module16</directory>
  </testsuite>
 </testsuites>

 <filter>
  <blacklist>
   <directory>./src</directory>
  </blacklist>
  <whitelist>
   <directory>./src/app/module</directory>
  </whitelist>
 </filter>

 <logging>
  <log type="coverage-html" target="build/module" title="module"
   charset="UTF-8" yui="true" highlight="true"
   lowUpperBound="70" highLowerBound="90"/>
  <log type="junit" target="build/logs/module.xml" logIncompleteSkipped="false"/>
 </logging>

</phpunit>
EN

回答 1

Stack Overflow用户

发布于 2018-03-07 07:57:19

嗯,你很少需要将白名单文件中不包含的东西列入黑名单--尽管这可能会节省你开始测试的时间。将您正在测试的代码列入白名单,然后对没有有用源代码的目录进行<exclude>

至于减少内存使用量,有一些技术将会有所帮助--首先,PHP7.1+将使用更少的内存。其次,找出哪些测试或代码占用了如此多的内存。当测试使用了大量未清理的内存时,像atrapalo/phpunit-memory-and-time-usage-listener这样的测试侦听器可以报告。您可能还会发现这对代码的改进。

其他像mybuilder/phpunit-accelerator这样的PHPUnit插件试图自动“取消设置”变量。在测试中使用您自己的tearDown()函数执行此操作也会有所帮助。

在短期内,如果您正在运行测试的机器上确实有大量RAM可用,您可以简单地增加限制:

代码语言:javascript
复制
<!-- phpunit.xml -->
<php>
    <ini name="memory_limit" value="-1" />
    <!-- other PHP.ini or environment variables -->
</php>

最后,如果您的代码库真的很大,您可以将测试和覆盖率报告分成不同的部分,将覆盖率报告写到使用phpcov工具合并的不同文件中。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49118107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档