dpbyr64224 2019-02-21 13:47
浏览 108

PHPUnit致命错误:允许的内存大小耗尽

When I run my tests, PHPUnit 8 crashes and generates a Fatal error: Allowed memory size of x bytes exhausted. I think it only crashes when some tests fail.

When I used it yesterday for the first time I set up 2 very simple tests comparing 2 strings. All worked fine when both tests passed. When I deliberately let 1 test fail it still worked. When both tests failed I got the specified error.

I found out that --process-isolation when executing PHPUnit 8 fixes the problem, however now it takes an incredible amount of time to execute the tests (5 minutes for about 30 simple string comparisons), increasing with every new test, which is obviously not feasible in the long run.

Also increasing the allowed memory really shouldn't be an answer to this problem because a) it would probably only be a temporary fix and b) it can't be that PHPUnit 8 uses all the available memory for only 2 simple failing tests.

I already uninstalled and reinstalled PHPUnit 8 which didn't affect the problem in any way.

Maybe important: I installed PHPUnit with Composer.

Update: Ok I just tried it out on my colleague's machine and it worked perfectly fine there with the same code base. This is getting weird...

This is the code I'm using to test my method:

class MyClassTest extends TestCase
{
    public function myFunctionProvider()
    {
        // horizontal tab
        $ht = "\u{0009}";
        ...

        return [
            'descr_1' => ['   abc  ', 'abc'  ],
            'descr_2' => ['d e  f'  , 'd e f'],
            'descr_3' => ['ghi'     , 'ghi'  ],

            'descr_4' => [$ht . $ht . 'jkl' . $ht               , 'jkl'    ],
            'descr_5' => ['m' . $ht . $ht , 'n' . $ht . 'o'     , 'm n o'  ],
            ...
        ];
    }

    /**
     * @dataProvider myFunctionProvider
     */
    public function testMyFunction(string $dirtyString, string $expectedResult): void
    {
        $actualResult = MyClass::myFunction($dirtyString);

        $this->assertEquals(
            $expectedResult,
            $actualResult
        );
    }
}

How can I get PHPUnit to not eat all my resources and function properly?

Edit: Based on Jakub Zalas' comment, here is the implementation of myFunction():

public static function myFunction(string $dirtyString): string
{                                                                          
    $regex = '/\s+| +/';                                                   
    $dirtyString = trim($dirtyString, $regex);                             
    $cleanedString = preg_replace($regex, ' ', $dirtyString);              

    return $cleanedString;                                                 
}      
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值